C 14 2017 5 31
1. 2. 3. 4. 5. 2/101 C
1.
( ) 4/101 C
C ASCII ASCII ASCII 5/101 C
10000 00100111 00010000 ASCII 10000 31H 30H 30H 30H 30H 1 0 0 0 0 0 ASCII 6/101 C
7/101 C
( ) ( ) 8/101 C
UNIX ANSI C 9/101 C
FILE 10/101 C
stdio.h typedef struct { int level; // unsigned flags; // char fd; // unsigned char hold; // int bsize; // unsigned char * buffer;// unsigned char * curp; // unsigned istemp; // short token; // } FILE; 11/101 C
FILE * FILE * fp; 12/101 C
2.
ANSI C fopen() 14/101 C
fopen() FILE * fopen(const char * path,const char * mode); path mode 15/101 C
FILE * fp; fp = fopen("file1", "r"); file1 file1 NULL 16/101 C
: "r" "w" "a" "rb" "wb" "ab" 17/101 C
(fopen) : "r+" / "w+" / "a+" / "rb+" / "wb+" / "ab+" / 18/101 C
"r" "r" "w" 19/101 C
"a" "r+" "w+" "a+" 20/101 C
fopen() "r" "r" "rb" "r+" "rb+" NULL FILE * fp; if ( (fp=fopen("file1", "r")) ==NULL ) { printf("cannot open this file\n"); exit(0); } 21/101 C
22/101 C
(fclose) 23/101 C
fclose() int fclose( FILE * fp ); fclose() 0 EOF(-1) 24/101 C
25/101 C
I 1 /* count.c -- using standard I/O */ 2 #include <stdio.h> 3 #include <stdlib.h> // ANSI C exit() prototype 4 5 int main(int argc, char *argv[]) 6 { 7 int ch; 8 // place to store each character as read 9 FILE *fp; 10 // "file pointer" 11 long count = 0; 12 13 if (argc!= 2) { 26/101 C
II 14 printf("usage: %s filename\n", argv[0]); 15 exit(1); 16 } 17 18 if ((fp = fopen(argv[1], "r")) == NULL) { 19 printf("can t open %s\n", argv[1]); 20 exit(1); 21 } 22 23 while ((ch = getc(fp))!= EOF) { 24 putc(ch,stdout); // same as putchar(ch); 25 count++; 26 } 27 fclose(fp); 27/101 C
III 28 printf("file %s has %ld characters\n", argv[1], count); 29 30 return 0; 31 } 28/101 C
3.
fputc() fgetc() fprintf() fscanf() fread() fwrite() 30/101 C
fputc() fputc() int fputc(int c, FILE * stream); fputc() c unsigned char stream c ASCII EOF 31/101 C
fgetc() fgetc() int fgetc(file * stream); fgetc() stream EOF. EOF feof(fp) feof(fp)==1 32/101 C
fgetc() 33/101 C
fgetc() I 1 // file2screen.c: 2 #include<stdio.h> 3 #include<stdlib.h> 4 5 int main(int argc, char * argv[]) 6 { 7 FILE * fp; 8 int ch; 9 10 if (argc!= 2) { 11 printf("usage: %s filename\n", argv[0]); 12 exit(1); 13 } 34/101 C
fgetc() II 14 15 if ((fp = fopen(argv[1], "r")) == NULL) { 16 printf("can t open %s\n", argv[1]); 17 exit(1); 18 } 19 20 while ((ch = fgetc(fp))!= EOF) { 21 putchar(ch); // same as putchar(ch); 22 } 23 24 return 0; 25 } 35/101 C
fgetc() while(!feof(fp)) { ch = fgetc(fp);... } 36/101 C
fputc() fgetc() # 37/101 C
fputc() fgetc() I 1 // screen2file.c: 2 #include <stdio.h> 3 #include <stdlib.h> 4 int main(void) 5 { 6 char ch; 7 FILE *fp; 8 9 if ( (fp = fopen("file1.txt", "w")) == NULL) { 10 printf("cannot open file1.txt!\n"); 11 exit(1); 12 } 13 while ((ch = getchar())!= # ) 38/101 C
fputc() fgetc() II 14 fputc(ch, fp); 15 fclose(fp); 16 17 return 0; 18 } 39/101 C
fputc() fgetc() 40/101 C
fputc() fgetc() I 1 // copy.c: 2 #include <stdio.h> 3 #include <stdlib.h> 4 int main(int argc, char * argv[]) 5 { 6 char ch; 7 FILE * fsrc, * fdes; 8 9 if (argc < 3) { 10 printf("usage: %s srcfile desfile\n", argv[0]); 11 exit(1); 12 } 13 41/101 C
fputc() fgetc() II 14 15 if ((fsrc = fopen(argv[1], "r")) == NULL 16 (fdes = fopen(argv[2], "w")) == NULL) { 17 printf("cannot open files!\n"); 18 exit(1); 19 } 20 21 while ((ch = getc(fsrc))!= EOF) 22 fputc(ch, fdes); 23 printf("successed copy srcfile to desfile\n"); 24 25 fclose(fsrc); 26 fclose(fdes); 27 42/101 C
fputc() fgetc() III 28 return 0; 29 } 43/101 C
fscanf() fprintf() fprintf() fscanf() printf() scanf() fprintf() fscanf() printf() scanf() 44/101 C
fscanf() fprintf() int fprintf(file * stream, const char *format,...); int fscanf (FILE * stream, const char *format,...); printf()/scanf() 45/101 C
fscanf() fprintf() fprintf(fp, "%d, %6.2f", i, t); i t %d %6.2f fp i=3, t=4.5 3, 4.50 46/101 C
fscanf() fprintf() fscanf() ASCII fscanf(fp, "%d, %f", &i, &t); 3, 4.5 3 i 4.5 t 47/101 C
fscanf() fprintf() 48/101 C
fscanf() fprintf() I 1 #include <stdio.h> 2 #include <stdlib.h> 3 #define MAX 40 4 int main(void) 5 { 6 FILE *fp; 7 char words[max]; 8 9 if((fp = fopen("words", "a+")) == NULL) { 10 fprintf(stdout, "Can t open \"words\" file.\n"); 11 exit(1); 12 } 13 49/101 C
fscanf() fprintf() II 14 puts("enter words to add to the file: press the Enter "); 15 puts("key at the beginning of a line to terminate." ); 16 while ( gets(words)!= NULL && words[0]!= \0 ) 17 fprintf(fp, "%s", words); 18 puts("file contents:"); 19 rewind(fp); 20 while(fscanf(fp, "%s", words)==1) 21 puts(words); 22 fclose(fp); 23 24 return 0; 25 } 50/101 C
fscanf() fprintf() III 51/101 C
fscanf() fprintf() fprintf() fscanf() ASCII fprintf() fscanf() fread() fwrite() 52/101 C
putw() getw() putw() getw() ( ) putw(10, fp); /* 10 fp */ i = getw(fp); /* fp i */ 53/101 C
fgets() fputs() fgets() fgets(str, n, fp); // fp n-1 str // str \0 fputs() fputs(str, fp); // str fp 54/101 C
fread() fwrite() num 8 0.333333 double num = 1./3.; fprintf(fp,"%f", num); %.2 4 0.33 %.12f 14 0.333333333333 55/101 C
fread() fwrite() num 0.33 fprintf() 56/101 C
fread() fwrite() double double fread() fwrite() 57/101 C
fread() fwrite() fwrite() size_t fwrite(const void * ptr, size_t size, size_t nmemb, FILE * fp); ptr size nmemb fp nmemb nmemb 58/101 C
fread() fwrite() 256 char buffer[256]; fwrite(buffer, 256, 1, fp) 256 59/101 C
fread() fwrite() 10 double double arr[10]; fwrite(arr, sizeof(double), 10, fp) arr 10 double 60/101 C
fread() fwrite() fread() size_t fread(const void * ptr, size_t size, size_t nmemb, FILE * fp); ptr size nmemb fp nmemb nmemb 61/101 C
fread() fwrite() 10 double double arr[10]; fread(arr, sizeof(double), 10, fp) 10 double arr 62/101 C
fread() fwrite() struct student_type { char name[10]; int num; int age; char addr[30]; } stu[40]; stu 40 63/101 C
fread() fwrite() for fread() 40 for(i=0; i<40; i++) fread(&stu[i], sizeof(struct student_type), 1, fp); fread(stu, sizeof(struct student_type), 40, fp); 64/101 C
fread() fwrite() for(i=0; i<40; i++) fwrite(&stu[i], sizeof(struct student_type), 1, fp); fwrite(stu, sizeof(struct student_type), 40, fp); 65/101 C
fread() fwrite() 66/101 C
fread() fwrite() I 1 #include <stdio.h> 2 #include <stdlib.h> 3 #define SIZE 4 4 void save(void); 5 6 struct stu_type 7 { 8 char name[20]; 9 int num; 10 int age; 11 char addr[15]; 12 } stu[size]; 13 67/101 C
fread() fwrite() II 14 int main(void) 15 { 16 int i; 17 FILE * fp; 18 printf("sizeof(structstud)=%d\n", (int) sizeof( struct stu_type)); 19 printf("please input the 4 student information, " 20 "including name, num, age, address\n"); 21 for(i = 0; i < SIZE; i++) 22 scanf("%s%d%d%s", stu[i].name, &stu[i].num, 23 &stu[i].age, stu[i].addr); 24 save(); 25 printf("\nthe information of the 4 students is:\n") ; 68/101 C
fread() fwrite() III 26 fp = fopen("stu_list.txt","rb"); 27 28 fread(stu, sizeof(struct stu_type), SIZE, fp); 29 for(i = 0; i < SIZE; i++) 30 { 31 // fread(&stu[i], sizeof(struct stu_type), 1, fp); 32 printf("%-10s%4d%4d%15s\n", stu[i].name, stu[i]. num, 33 stu[i].age, stu[i].addr); 34 } 35 fclose(fp); 36 37 return 0; 38 } 69/101 C
fread() fwrite() IV 39 40 void save(void) 41 { 42 FILE * fp; 43 int i; 44 45 if ((fp = fopen("stu_list.txt", "wb")) == NULL) { 46 printf("cannot open file!\n"); 47 exit(1); 48 } 49 for (i = 0; i < SIZE; i++) { 50 if (fwrite(&stu[i], sizeof(struct stu_type), 1, fp)!= 1) 51 printf("file write error.\n"); 70/101 C
fread() fwrite() V 52 } 53 fclose(fp); 54 55 } 71/101 C
fread() fwrite() 72/101 C
fread() fwrite() I 1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 float buffer[] = {1.0, 2.0, 3.0}; 6 float read[3]; 7 FILE * fp; 8 9 if ((fp = fopen("file3.txt", "wb")) == NULL) { 10 printf("cannot open files.\n"); 11 exit(0); 12 } 13 fwrite(buffer, 1, sizeof(buffer), fp); 73/101 C
fread() fwrite() II 14 fclose(fp); 15 16 if ((fp = fopen("file3.txt", "rb")) == NULL) { 17 printf("cannot open files.\n"); 18 exit(0); 19 } 20 fread(read, 1, sizeof(read), fp); 21 printf("%f %f %f\n", read[0], read[1], read[2]); 22 fclose(fp); 23 24 return 0; 25 } 74/101 C
4.
ftell() rewind()/fseek() 76/101 C
rewind() void rewind(file * stream); fp 77/101 C
rewind() 78/101 C
rewind() I 1 // rewind.c: 2 #include <stdio.h> 3 #include <stdlib.h> 4 int main(int argc, char * argv[]) 5 { 6 int ch; 7 FILE * fsrc, * fdes; 8 9 if (argc < 3) { 10 printf("usage: %s srcfile desfile\n", argv[0]); 11 exit(1); 12 } 13 79/101 C
rewind() II 14 if ((fsrc = fopen(argv[1], "r")) == NULL 15 (fdes = fopen(argv[2], "w")) == NULL) { 16 printf("cannot open files!\n"); 17 exit(1); 18 } 19 while ((ch = getc(fsrc))!= EOF) 20 putchar(ch); 21 22 rewind(fsrc); 23 while ((ch = getc(fsrc))!= EOF) 24 fputc(ch, fdes); 25 26 fclose(fsrc); 27 fclose(fdes); 80/101 C
rewind() III 28 29 return 0; 30 } 81/101 C
rewind() file.txt C primer plus C programming $ gcc rewind.c -o rewind $./rewind file.txt file1.txt C primer plus C programming 82/101 C
fseek() fseek() int fseek(file * stream, long offset, int fromwhere); fseek() 0-1 83/101 C
fseek() SEEK_SET 0 SEEK_CUR 1 SEEK_END 2 84/101 C
fseek() fseek(fp, 0L, SEEK_SET); // fseek(fp, 10L, SEEK_SET); // 10 fseek(fp, 2L, SEEK_CUR); // //2 fseek(fp, 0L, SEEK_END); // fseek(fp, -10L, SEEK_END);// 10 85/101 C
ftell() ftell() ANSI C i = ftell(fp); if(i == -1L) printf("error\n"); i ( fp ) "error" 86/101 C
fseek() ftell() 87/101 C
fseek() ftell() I 1 // reverse.c 2 #include<stdio.h> 3 #include<stdlib.h> 4 #define CNTL_Z \032 5 #define SLEN 50 6 int main(void) 7 { 8 char file[slen]; 9 char ch; 10 FILE * fp; 11 long count, last; 12 88/101 C
fseek() ftell() II 13 puts("enter the name of the file to be processed: " ); 14 gets(file); 15 16 if ((fp = fopen(file, "rb"))==null) { 17 printf("reverse can t be open %s\n", file); 18 exit(1); 19 } 20 21 fseek(fp, 0L, SEEK_END); 22 last = ftell(fp); 23 24 for (count = 1L; count <= last; count++) { 25 fseek(fp, -count, SEEK_END); 89/101 C
fseek() ftell() III 26 ch = getc(fp); 27 if (ch!= CNTL_Z && ch!= \r ) 28 putchar(ch); 29 } 30 31 putchar( \n ); 32 fclose(fp); 33 34 return 0; 35 } 90/101 C
fseek() ftell() // file4 Hello World! I love WHU! Enter the name of the file to be processed: file4!uhw evol I!dlroW olleh 91/101 C
fseek() ftell() double 92/101 C
fseek() ftell() I 1 // randbin.c 2 #include <stdio.h> 3 #include <stdlib.h> 4 #define SIZE 1000 5 6 int main(void) 7 { 8 double arr[size]; 9 double value; 10 const char * file = "number.dat"; 11 int i; 12 long pos; 13 FILE * fp; 93/101 C
fseek() ftell() II 14 15 // Creat an array with double elements 16 for (i = 0; i < SIZE; i++) 17 arr[i] = 100.0 * i + 1.0 / (i + 1); 18 19 // Attempt to open file 20 if ((fp = fopen(file, "wb")) == NULL) { 21 fprintf(stderr, "Could not open %s for output", file); 22 exit(1); 23 } 24 25 // Write the data in the array into file with binary mode 94/101 C
fseek() ftell() III 26 fwrite(arr, sizeof(double), SIZE, fp); 27 28 // Close the file 29 fclose(fp); 30 31 // Attempt to open file 32 if ((fp = fopen(file, "rb")) == NULL) { 33 fprintf(stderr, "Could not open %s for random acess", file); 34 exit(1); 35 } 36 37 // Read selected item in the file 95/101 C
fseek() ftell() IV 38 printf("enter an index in the range 0-%d\n", SIZE -1); 39 scanf("%d", &i); 40 41 while(i >= 0 && i < SIZE) { 42 pos = (long) i * sizeof(double); // compute offset 43 fseek(fp, pos, SEEK_SET); 44 fread(&value, sizeof(double), 1, fp); 45 printf("the value there is %f.\n", value); 46 printf("next index (out of range to quit):\n"); 47 scanf("%d", &i); 48 } 49 fclose(fp); 96/101 C
fseek() ftell() V 50 puts("bye!"); 51 52 return 0; 53 } 97/101 C
fseek() ftell() I Enter an index in the range 0-999 1 The value there is 100.500000. Next index (out of range to quit): 4 The value there is 400.200000. Next index (out of range to quit): 5 The value there is 500.166667. Next index (out of range to quit): 100 The value there is 10000.009901. Next index (out of range to quit): 98/101 C
fseek() ftell() II -1 Bye! 99/101 C
5.
C 101/101 C
ferror() fopen() NULL ferror() int ferror(file * stream); 0 0 102/101 C
cleanerr int ferror(file * stream); 0 ferror(fp) 0 clearerr(fp) ferror(fp) 0 103/101 C