1 7 C
2 7.1
C (System Calls) Unix UNIX man Section 2 C. C (Library Functions) C 1975 Dennis Ritchie ANSI C Standard Library 3
(system call). 4
C?... 5
C (text file), C. (binary file). 6
C 1. : fopen( ) 2. : 3. : fclose( ) 7
(fopen). FILE FILE. fopen() FILE *fopen(const char *filename, const char *mode); const char *filename: const char *mode: FILE *fp; fp = fopen( ~/sp/text.txt", "r"); if (fp == NULL) { printf(" \n"); } fp = fopen("outdata.txt", "w"); fp = fopen("outdata.txt", "a"); 8
fopen (): "r" (read) NULL "w" (write) "a" (append) "r+" NULL "w+" "a+" 9
FILE (file descriptor) C fopen( ) FILE (FILE *) FILE FILE #include <stdio.h> FILE, 10
FILE FILE : typedef struct { int cnt; unsigned char*base; unsigned char*ptr; // // // unsinged flag; int fd; } FILE; // FILE // ( _IOFBF, _IOLBF, _IONBUF, // _IOEOF, _IOERR _IOREAD, _IOWRT) // 11
/ / I/O (stream) open stdin, stdout, stderr FILE* #include <stdio.h> stdin FILE stdout FILE stderr FILE 12
. int fclose(file *fp ); fp fopen 0, EOF( -1). fclose(fp); 13
7.2
getchar() fgetc(), getc() putchar() fputc(), putc() gets() fgets() puts() fputs() scanf() fscanf() printf() fprintf()
fgetc() fputc(). int fgetc(file *fp); getc fp. EOF(-1). int fputc(int c, FILE *fp); putc EOF(-1) 16
cat.c #include <stdio.h> /* */ int main(int argc, char *argv[]) { FILE *fp; int c; if (argc < 2) fp = stdin; // else fp = fopen(argv[1],"r"); // c = getc(fp); // while (c!= EOF) { // putc(c, stdout); // c = getc(fp); // } fclose(fp); return 0; } 17
copy.c ( ) #include <stdio.h> /* */ int main(int argc, char *argv[]) { char c; FILE *fp1, *fp2; if (argc!=3) { fprintf(stderr, " : %s 1 2\n", argv[0]); return 1; } 18
copy.c fp1 = fopen(argv[1], "r"); if (fp1 == NULL) { fprintf(stderr, " %s \n", argv[1]); return 2; } fp2 = fopen(argv[2], "w"); while ((c = fgetc(fp1))!= EOF) fputc(c, fp2); } fclose(fp1); fclose(fp2); return 0; 19
int feof(file *fp) fp 0 0. int ungetc(int c, FILE *p) c. 1. int fflush(file *fp) fp.. 20
fgets() fputs(). char* fgets(char *s, int n, FILE *fp); s s ('\n') EOF n-1 NULL. NULL. int fputs(const char *s, FILE *fp); s fp EOF 21
line.c 1 #include <stdio.h> 2 #define MAXLINE 80 3 4 /*. */ 5 int main(int argc, char *argv[]) 6 { 7 FILE *fp; 8 int line = 0; 9 char buffer[maxline]; 10 11 if (argc!= 2) { 12 fprintf(stderr, " :line \n"); 13 exit(1); 14 } 22
line.c 15 16 if ( (fp = fopen(argv[1],"r")) == NULL) 17 { 18 fprintf(stderr, " 23 line++; 19 exit(2); 20 } 21 22 while (fgets(buffer, MAXLINE, fp)!= NULL) { // 23 line++; 24 printf("%3d %s", line, buffer); 25 } 26 exit(0); 27 } 23
fprintf() printf(). fscanf() scanf(). int fprintf(file *fp, const char *format,...); fprintf fp FILE printf int fscanf(file *fp, const char *format,...); fscanf fp FILE scanf 24
fprint.c #include <stdio.h> #include "student.h" /*. */ int main(int argc, char* argv[]) { struct student rec; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n", argv[0]); return 1; } fp = fopen(argv[1], "w"); printf("%-9s %-7s %-4s\n", " ", " ", " "); while (scanf("%d %s %d", &rec.id, rec.name, &rec.score)==3) fprintf(fp, "%d %s %d ", rec.id, rec.name, rec.score); fclose(fp); return 0; } 25
fscan.c #include <stdio.h> #include "student.h" /*. */ int main(int argc, char* argv[]) { struct student rec; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n", argv[0]); return 1; } fp = fopen(argv[1], "r"); printf("%-9s %-7s %-4s\n", " ", " ", " "); while (fscanf(fp,"%d %s %d", &rec.id, rec.name, &rec.score)==3) printf("%10d %6s %6d\n", rec.id, rec.name, rec.score); fclose(fp); return 0; } 26
7.3
fopen(): "rb" (read) NULL "wb" (write) "ab" (append) "rb+" NULL "wb+" "ab+" 28
fread() fwrite() int fread(void *buf, int size, int n, FILE *fp); fp size ( ) n buf int fwrite(const void *buf, int size, int n, FILE *fp); fp buf size ( ) n 29
fwrite() 30
: rec struct student rec; FILE *fp = fopen( stfile", "wb"); fwrite(&rec, sizeof(rec), 1, fp); 31
stcreate1. c #include <stdio.h> #include "student.h" int main(int argc, char* argv[]) { struct student rec; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n",argv[0]); exit(1); } student.h struct student { int id; char name[20]; short score; }; fp = fopen(argv[1], "wb"); printf("%-9s %-7s %-4s\n", " ", " ", " "); while (scanf("%d %s %d", &rec.id, rec.name, &rec.score) == 3) fwrite(&rec, sizeof(rec), 1, fp); } fclose(fp); exit(0); 32
fread() 33
stprint.c #include <stdio.h> #include "student.h" /*. */ int main(int argc, char* argv[]) { struct student rec; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n", argv[0]); return 1; } if ((fp = fopen(argv[1], "rb")) == NULL ) { fprintf(stderr, " \n"); return 2; } 34
stprint.c printf("-----------------------------------\n"); printf("%10s %6s %6s\n", " ", " ", " "); printf("-----------------------------------\n"); while (fread(&rec, sizeof(rec), 1, fp) > 0) if (rec.id!= 0) printf("%10d %6s %6d\n", rec.id, rec.name, rec.score); } printf("-----------------------------------\n"); fclose(fp); return 0; 35
7.4 36
(current file position) (file position pointer). 37
fseek(file *fp, long offset, int mode) rewind(file *fp). ftell(file *fp) 38
fseek() fseek(file *fp, long offset, int mode) FILE fp (mode) (offset). SEEK_SET 0 SEEK_CUR 1 SEEK_END 2 39
fseek() fseek(fd, 0L, SEEK_SET); (rewind) fseek(fd, 100L, SEEK_SET); 100 fseek(fd, 0L, SEEK_END); (append) fseek(fd, n * sizeof(record), SEEK_SET); n+1 fseek(fd, sizeof(record), SEEK_CUR); fseek(fd, -sizeof(record), SEEK_CUR); fseek(fd, sizeof(record), SEEK_END); 40
: fwrite(&record1, sizeof(record), 1, fp); fwrite(&record2, sizeof(record), 1, fp); fseek(fd, sizeof(record), SEEK_END); fwrite(&record3, sizeof(record), 1, fp); #1 #2 #3 41
stcreate2.c #include <stdio.h> #include "student.h" #define START_ID 1201001 /*. */ int main(int argc, char* argv[]) { struct student rec; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n", argv[0]); exit(1); } 42
stcreate2.c } fp = fopen(argv[1], "wb"); printf("%7s %6s %4s\n", " ", " ", " "); while (scanf("%d %s %d", &rec.id, rec.name, &rec.score) == 3) { fseek(fp, (rec.id START_ID)* sizeof(rec), SEEK_SET); fwrite(&rec, sizeof(rec), 1, fp); } fclose(fp); exit(0); 43
stquery.c #include <stdio.h> #include "student.h" int main(int argc, char *argv[]) { struct student rec; char c; int id; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n", argv[0]); exit(1); } if ((fp = fopen(argv[1], "rb")) == NULL ) { fprintf(stderr, " \n"); exit(2); } 44
stquery.c do { printf(" : "); if (scanf("%d", &id) == 1) { fseek(fp, (id - START_ID) *sizeof(rec), SEEK_SET); if ((fread(&rec, sizeof(rec), 1, fp) > 0) && (rec.id!= 0)) printf(" : %8d : %4s : %4d\n", rec.id, rec.name, rec.score); else printf(" %d \n", id); } else printf(" "); printf("?(y/n)"); scanf(" %c", &c); } while (c == 'Y'); } fclose(fp); exit(0); 45
(1) (2) (3). 46
stupdate.c #include <stdio.h> #include "student.h" /*. */ int main(int argc, char *argv[]) { struct student rec; int id; char c; FILE *fp; if (argc!= 2) { fprintf(stderr, " : %s \n", argv[0]); exit(1); } if ((fp = fopen(argv[1], "rb+")) == NULL) { fprintf(stderr, " \n"); exit(2); } 47
stupdate.c do { printf(" : "); if (scanf("%d", &id) == 1) { fseek(fp, (id - START_ID) * sizeof(rec), SEEK_SET); if ((fread(&rec, sizeof(rec), 1, fp) > 0)&&(rec.id!= 0)) { printf(" : %8d : %4s : %4d\n", rec.id, rec.name, rec.score); printf(" : "); scanf("%d", &rec.score); fseek(fp, -sizeof(rec), SEEK_CUR); fwrite(&rec, sizeof(rec), 1, fp); } else printf(" %d \n", id); } else printf(" \n"); printf("?(y/n)"); scanf(" %c",&c); } while (c == 'Y'); fclose(fp); exit(0); } 48
49 7.5
C C I/O read (), write () I/O C (fully buffered) (line buffered) (unbuffered) 50
C I/O (newline) I/O (stdin, stdout). (stderr) 51
buffer.c #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *fp; if (!strcmp(argv[1], "stdin")) { fp = stdin; printf(" :"); if (getchar() == EOF) perror("getchar"); } else if (!strcmp(argv[1], "stdout")) fp = stdout; else if (!strcmp(argv[1], "stderr")) fp = stderr; else if ((fp = fopen(argv[1], "r")) == NULL) { perror("fopen"); exit(1); } else if (getc(fp) == EOF) perror("getc"); 52
buffer.c printf(" = %s, ", argv[1]); if (fp->_flags & _IO_UNBUFFERED) printf(" "); else if (fp->_flags & _IO_LINE_BUF) printf(" "); else printf(" "); } printf(", = %d\n", fp->_io_buf_end - fp->_io_buf_base); exit(0); 53
setbuf()/setvbuf() #include <stdio.h> void setbuf (FILE *fp, char *buf ); int setvbuf (FILE *fp, char *buf, int mode, size_t size );, 54
setbuf() void setbuf (FILE *fp, char *buf ); on/off. buf NULL buf BUFSIZ / 55
setvbuf() int setvbuf (FILE *fp, char *buf, int mode, size_t size ); : 0, nonzero mode _IOFBF : _IOLBF : _IONBF : 56
setvbuf() mode == _IONBF buf size mode == _IOLBF or _IOFBF buf NULL buf size NULL stat st_blksize (disk files) st_blksize BUFSIZ (pipes) 57
:setbuf #include <stdio.h> main() { printf(", "); sleep(1); printf("!"); sleep(1); printf(" \n"); sleep(1); setbuf(stdout, NULL); printf(", "); sleep(1); printf(" "); sleep(1); printf(" ^^"); sleep(1); printf(" \n"); sleep(1); } 58
:setvbuf #include <stdio.h> int main( void ) { char buf[1024]; FILE *fp1, *fp2; fp1 = fopen("data1", "a"); fp2 = fopen("data2", "w"); if( setvbuf(fp1, buf, _IOFBF, sizeof( buf ) )!= 0 ) printf(" : \n" ); else printf(" : 1024 \n" ); } if( setvbuf(fp2, NULL, _IONBF, 0 )!= 0 ) printf(" : \n" ); else printf(" : \n" ); 59
fflush() #include <stdio.h> int fflush (FILE *fp); fp write() : 0, EOF (-1) fp NULL, 60
. fopen() FILE. FILE. fgetc() fputc(). fgets() fputs(). fread() fwrite().. fseek(). 61