include stdio h 12 1 voidinclude main stdio

  • Slides: 19
Download presentation

#include <stdio. h> 例12. 1 从键盘输入字符,逐个 void#include main() <stdio. h> 存到磁盘文件中,直到 void main() {

#include <stdio. h> 例12. 1 从键盘输入字符,逐个 void#include main() <stdio. h> 存到磁盘文件中,直到 void main() { FILE *in, *out; 输入‘#“为止 { FILE *fp; char ch, infile[10], outfile[10]; 判断二进制文件是否结束 char ch, *filename=“out. txt”; #include <stdio. h> scanf("%s", infile); while(!feof(fp)) if((fp=fopen(filename, "w"))==NULL) void main() scanf("%s", outfile); { c=fgetc(fp); open filen"); { printf("cannot { FILE *fp; if ((in = fopen(infile, "r"))== NULL) ……. . 例_1 读文本文件内容, { printf("Cannot charexit(0); ch, *filename=“out. txt”; open infile. n"); } } if((fp=fopen(filename, ”r"))==NULL) 并显示 exit(0); inputopen string: "); { printf("cannot filen"); } printf("Please ch=getchar(); 例12_2 文件拷贝 if ((out =exit(0); fopen(outfile, "w"))== NULL) while(ch!='#') } { printf("Cannot open outfile. n"); • feof { while((ch=fgetc(fp))!=EOF) fputc(ch, fp); exit(0); – 函数原型: feof(fp)putchar(ch); } – 功能:判断文件是否结束 ch=getchar(); whilefclose(fp); (!feof(in)) 判断文本文件是否结束 – 返值:文件结束,返回真(非 0);文件未结束,返回 0 }} fputc(fgetc(in), out); fclose(fp); fclose(in); fclose(out); } }

例 float f[2]; FILE *fp; fp=fopen(“aa. dat”, “rb”); for(i=0; i<2; i++) fread(&f[i], 4, 1,

例 float f[2]; FILE *fp; fp=fopen(“aa. dat”, “rb”); for(i=0; i<2; i++) fread(&f[i], 4, 1, fp); fread(f, 4, 2, fp); 例 struct student { int num; char name[20]; char sex; int age; float score[3]; }stud[10]; for(i=0; i<10; i++) fread(&stud[i], sizeof(struct student), 1, fp);

例12_3 从键盘输入 4个学生数据,把他们转存到磁盘文件中去 voidsave() display() #include <stdio. h> void {{ FILE*fp; #define SIZE 2

例12_3 从键盘输入 4个学生数据,把他们转存到磁盘文件中去 voidsave() display() #include <stdio. h> void {{ FILE*fp; #define SIZE 2 int i; i; struct student_type if((fp=fopen("d: \fengyi\exe\stu_dat", "wb"))==NULL) if((fp=fopen("d: \fengyi\exe\stu_dat", "rb"))==NULL) { char name[10]; {{ printf("cannotopenfilen"); int num; return; int age; }} char addr[15]; for(i=0; i<SIZE; i++) }stud[SIZE]; student_type), 1, fp)!=1) { if(fwrite(&stud[i], sizeof(struct fread(&stud[i], sizeof(struct student_type), 1, fp); main() printf("file write errorn"); printf("%-10 s %4 d %-15 sn", stud[i]. name, { fclose(fp); stud[i]. num, stud[i]. age, stud[i]. addr); int i; } } for(i=0; i<SIZE; i++) fclose(fp); scanf("%s%d%d%s", stud[i]. name, &stud[i]. num, } &stud[i]. age, stud[i]. addr); save(); display(); }

– 格式化I/O: fprintf与fscanf #include <stdio. h> • 函数原型: main() *fp, const char *format[, argument,

– 格式化I/O: fprintf与fscanf #include <stdio. h> • 函数原型: main() *fp, const char *format[, argument, …]) int fprintf(FILE { char s[80], c[80]; int fscanf(FILE *fp, const char *format[, address, …]) int a, b; • 功能:按格式对文件进行I/O操作 FILE *fp; • 返值:成功, 返回I/O的个数; 出错或文件尾, 返回EOF if((fp=fopen("test", "w"))==NULL) { puts("can't open file"); exit() ; } 例 fprintf(fp, “%d, %6. 2 f”, i, t); //将i和t按%d, %6. 2 f格式输出到fp文件 fscanf(stdin, "%s%d", s, &a); /*read from keaboard*/ fscanf(fp, “%d, %f”, &i, &t); //若文件中有3, 4. 5 , 则将3送入i, 4. 5送入t fprintf(fp, "%s %d", s, a); /*write to file*/ fclose(fp); 例_2 从键盘按格式输入数据存到磁盘文件中去 if((fp=fopen("test", "r"))==NULL) { puts("can't open file"); exit(); } fscanf(fp, "%s%d", c, &b); /*read from file*/ fprintf(stdout, "%s %d", c, b); /*print to screen*/ fclose(fp); }

– 字符串I/O: fgets与fputs #include<stdio. h> *s, int n, FILE *fp) • 函数原型: char *fgets(char

– 字符串I/O: fgets与fputs #include<stdio. h> *s, int n, FILE *fp) • 函数原型: char *fgets(char main() { FILE *fp; int fputs(char *s, FILE *fp) char string[81]; • 功能:从fp指向的文件读/写一个字符串 if((fp=fopen("file. txt", "w"))==NULL • 返值: fputs把s指向的字符串写入fp指向的文件 { printf("cann't open file"); exit(0); } fgets从fp所指文件读n-1个字符送入s指向的内存区, – fgets正常时返回读取字符串的首地址;出错或文件尾,返回 while(strlen(gets(string))>0) 并在最后加一个‘’ NULL { fputs(string, fp); (若读入n-1个字符前遇换行符或文件尾(EOF)即结束) – fputs正常时返回写入的最后一个字符;出错为EOF fputs("n", fp); 例_3 从键盘读入字符串存入文件,再从文件读回显示 } fclose(fp); if((fp=fopen("file. txt", "r"))==NULL) { printf("cann't open file"); exit(0); } while(fgets(string, 81, fp)!=NULL) fputs(string, stdout); fclose(fp); }

main() – fseek函数 { int#include"stdio. h" i; main() FILEint *fp; • 函数原型: fseek(FILE *fp,

main() – fseek函数 { int#include"stdio. h" i; main() FILEint *fp; • 函数原型: fseek(FILE *fp, long offset, int whence)if((fp=fopen("studat", "rb"))==NULL) { FILE *fp; char filename[80]; { printf("can't open filen"); exit(0); } • 功能:改变文件位置指针的位置 long length; for(i=0; i<3; i+=2) • 返值:成功,返回 0;失败,返回非 0值 gets(filename); { fseek(fp, i*sizeof(struct 文件指针起始点 student_type), 0); 位移量(以起始点为基点, 移动的字节数) 例 fseek(fp, 100 L, 0); – ftell函数 文件开始 SEEK_SET 0 fp=fopen(filename, "rb"); fread(&stud[i], sizeof(struct student_type), 1, fp); >0 向后移动 fseek(fp, 50 L, 1); • 函数原型: printf("%s long ftell(FILE *fp) 文件当前位置 SEEK_CUR 1 if(fp==NULL) %d %d <0 向前移动 fseek(fp, -10 L, 2); %sn", • 功能:返回位置指针当前位置(用相对文件开头的位移 文件末尾 printf("file not found!n"); SEEK_END 2 stud[i]. name, stud[i]. num, stud[i]. age, stud[i]. addr); 量表示) } else • 返值:成功,返回当前位置指针位置;失败,返回-1 L, { fseek(fp, 0 L, SEEK_END); #include <stdio. h> fclose(fp); struct student_type length=ftell(fp); } 例 磁盘文件上有3个学生数据,要求读入第 1,3学生数据并显示 int num; printf("Length of File is %1 d{ bytesn", length); char name[10]; fclose(fp); int age; 例 求文件长度 } char addr[15]; } }stud[3];

#include <stdio. h> – clearerr函数 int main(void) 函数原型: void clearerr(FILE *fp) { FILE *stream;

#include <stdio. h> – clearerr函数 int main(void) 函数原型: void clearerr(FILE *fp) { FILE *stream; 功能:使文件错误标志置为 0 stream = fopen("DUMMY. FIL", "w"); 返值:无 getc(stream); 说明:出错后,错误标志一直保留,直到对同一 if (ferror(stream)) 文件调clearerr(fp)或rewind或任何其它一个输 { printf("Error reading from DUMMY. FILn"); 入输出函数 clearerr(stream); } 例 ferror()与clearerr()举例 if(!ferror(stream)) printf("Error indicator cleared!"); fclose(stream); return 0; } • •