Chapter 17
fopen() fopen() fopen() stdio.h fopen() f (file) f
fopen() , FILE * FILE * FILE stdio.h , FILE * FILE struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname;};typedef struct _iobuf FILE;
fopen() , () basic.txt , w fopen() (open), FILE f fopen() NULL
if fopen() " .\n" f fclose(f) fopen() ()r, w, a, r+, w+, a+ FILE *f; char fname[] = "basic.txt";if ((f = fopen(fname, "w")) == NULL) { printf( " .\n" );}; fclose(f);
fprintf() fopen() , . C fprintf() fprintf() printf() ,
f " %s %d .\n" name point fprintf(f, " %s %d .\n", name, point);
Filebasic.c fopen(), fprintf() basic.txt exit() exit() stdlib.h exit() , , 0 , 0 if ((f = fopen(fname, "w")) == NULL) { printf( " .\n" ); exit(1);};
fprint, fscanf fprintf() fscanf() stdio.h fprintf() fscanf()
, , fprintf() fscanf() fprintf() fscanf() stdin stdout , stdin, stdout stderr stdio.h , , int fprintf(FILE *, const char *, ...);int fscanf(FILE *, const char *, ...);#define stdin (&_iob[0])#define stdout (&_iob[1])#define stderr (&_iob[2])
, , , , if ((f = fopen(fname, "w")) == NULL) {printf( " .\n" ); exit(1);};printf(" (, ) .\n" );scanf("%s%d%d", name, &point1, &point2);
, , fprintf() f cnt
r
// "grade.txt" fprintf(f, "%d %s %d %d\n", ++cnt, name, point1, point2);fclose(f);if ((f = fopen(fname, "r")) == NULL) { printf( " .\n" ); exit(1);};// "grade.txt" fscanf(f, "%d %s %d %d\n", &cnt, name, &point1, &point2);
fscanf() , fprintf() fscanf()
fprintf() stdout printf() // fprintf(stdout, "\n%6s%18s%8s%8s\n", "", "", "", ""); fprintf(stdout, "%6d%18s%8d%8d\n", cnt, name, point1, point2);fclose(f);printf("%6d%18s%8d%8d\n", cnt, name, point1, point2);
Filewrite.c , grade.txt
fgets() fputs() fgets() fputs() fgets() fputs() stdio.h fgets() (\n) fputs() fgets() , , fputs() , char * fgets(char *, int, FILE *);int fputs(char *, FILE *);
fgetc() fputc() fgetc() fputc() stdio.h
getc() putc()
getchar() putchar() getc() putc()
fgetc() fputc() getc() putc() , fgetc() fputc() , getc() putc() int fgetc(FILE *);int fputc(int, FILE *); int getc(FILE *);int putc(int, FILE *);#define getchar() getc(stdin)#define putchar(_c) putc((_c),stdout)
feof() ferror() feof() (EOF) stdio.h
(end of file) , 0 , 0
ferror() stdio.h
0 , 0 int feof(FILE *);while (!feof(stdin)) { }int ferror(FILE *);
fgets() fputs()
fgets() , fputs() f
char fname[] = "grade.txt";char names[80];FILE *f; printf(" (, ) .\n" );fgets(names, 80, stdin);while (!feof(stdin)) {// "grade.txt" fprintf(f, "%d ", ++cnt); fputs(names, f);fgets(names, 80, stdin);}fclose(f);
printhead()
f feof() , fscanf() fprintf() fopen() , fclose() printhead();while (!feof(f)) {// "grade.txt" fscanf(f, "%d %s %d %d\n", &cnt, names, &point1, &point2);// fprintf(stdout, "%6d%18s%8d%8d\n", cnt, names, point1, point2);}
type . list list filename filename , ,
2
argv[1] if (argc != 2) {printf(" : list filename\n" ); exit(1);}; if ((f = fopen(argv[1], "r")) == NULL) {printf( " .\n" ); exit(1);};
, fgetc(f) f , ch putchar(ch) putchar(ch) putc(ch, stdout)
printf() printf("%4d: ", ++cnt);while ((ch = fgetc(f)) != EOF) {putchar(ch);if (ch == '\n') {printf("%4d: ", ++cnt);};}printf("\n");fclose(f);
List.c list list filename filename
fprintf() fscanf() () , ( ) , fprintf() cnt f
fprintf() int cnt , 10 fprintf() , f 10 1 0 (binary) C ,
fwrite fread (binary) fwrite() fread() fwrite() stdio.h
fread() fwrite() , , , void * void * size_t fwrite(const void *, size_t, size_t, FILE *); size_t fread(void *, size_t, size_t, FILE *);
fwrite() cnt f
fwrite() fwrite() fread()
Scorefile.c , score.bin personscore , , , , score.h
, , , , , fgets() , sscanf() sscanf() printf(" (, , ) .\n" );fgets(line, 80, stdin);while (!feof(stdin)) { // sscanf(line, "%s %d %d %d", score.name,&score.mid, &score.final, &score.quiz); score.number = ++cnt;fwrite(&score, sizeof(pscore), 1, f);fgets(line, 80, stdin);}
Readscorefile.c score.bin score.h ,
r
fread() fread() score
feof() fprintf() if ((f = fopen(fname, "r")) == NULL) {printf( " .\n" );exit(1);};fread(&score, sizeof(pscore), 1, f);while (!feof(f)) { // fprintf(stdout, "%6d%18s%8d%8d%8d\n", score.number, score.name, score.mid, score.final, score.quiz); fread(&score, sizeof(pscore), 1, f);}
(file pointer) 100L 100 long 100L L , (Random Access) fseek()
fseek()fseek() fseek() stdio.h fseek() fptr mode offset offset fseek() long (offset) fseek() int fseek(FILE *fptr, long offset, int mode)
fseek(f, 100L, SEEK_SET) 100 .
fseek(f, 100L, SEEK_CUR) 100 .
fseek(f, -100L, SEEK_END) 100 .
fseek() , ,
ftell() rewind()
Appendscore.c readscorefile.c score.bin 1
score.bin ctrl + z , score.bin score.bin a+ . a+ , score.bin ,
printscore() .
if ((f = fopen(fname, "a+")) == NULL) {printf( " .\n" ); exit(1);}; printscore(f);
score fseek()
appendscore() printscore()
offset = (long) sizeof(pscore); fseek(f, -offset, SEEK_CUR);fread(&score, sizeof(pscore), 1, f);// cnt = score.number;printf("\n %d . \n\n", cnt);fseek(f, 0L, SEEK_END);appendscore(f, cnt);printscore(f);fclose(f);
printscore() , rewind() 0 void printscore(FILE *f) {pscore score;rewind(f);printhead(); fread(&score, sizeof(pscore), 1, f);while (!feof(f)) {}}
, remove() remove() remove(sample.txt) rename() rename() rename(oldname.txt, newname.txt) remove() rename() stdio.h
filepro.c c filename , getw()
>fileproc c filename
d filename .
>fileproc d filename n filename1 filename2
>fileproc n filename1 filename2
d remove() , n rename()
c c getw() 1 putw() 1 argv[1] mode if (strcmp(argv[1], "-c") == 0) {mode = 1;} else if (strcmp(argv[1], "-d") == 0) {mode = 2;} else if (strcmp(argv[1], "-n") == 0) {mode = 3;};
c , r+ , w+ switch (mode) { case 1: if ((f = fopen(argv[2], "r+")) == NULL) {if ((f = fopen(argv[2], "w+")) == NULL) {printf( " .\n" );exit(1); } };
0 , getw()
cnt 1 ++cnt putw() fseek(f, 0L, SEEK_END);if (ftell(f) != 0) {fseek(f, 0L, SEEK_SET);cnt = getw(f);}fseek(f, 0L, SEEK_SET);putw(++cnt, f);printf("-c %s %d .\n", argv[2], cnt);fclose(f);break;
d remove(argv[2])
-n rename(argv[2], argv[3]) case 2:remove(argv[2]);break;case 3:if (argc != 4) {printf(" : -[c,d,n] filename\n" ); exit(1);};rename(argv[2], argv[3]); break;
score.bin score.h , printscore() printhead() , searchscore()
score.bin . . . searchscore(FILE *f, int cnt) , . .
,
ftell() fseek(f, 0L, SEEK_END);max = ftell(f) / sizeof(pscore);printf("\n %d . >>\n", max); printf(" . >> ");scanf("%d", &cnt);if (1
cnt (SEEK_SET) sizeof(pscore) * (cnt-1) fseek() , fread() void searchscore(FILE *f, int cnt) {pscore score;long offset = (long) (sizeof(pscore) * (cnt-1)); fseek(f, offset, SEEK_SET); fread(&score, sizeof(pscore), 1, f);printf("\n >>\n\n");fprintf(stdout, "%6s%18s%8s%8s%8s\n", "", "", "", "", ""); fprintf(stdout, "%6d%18s%8d%8d%8d\n", score.number, score.name, score.mid, score.final, score.quiz);}