文件打开类型:
文件打开输出就用:
1 #include <stdio.h> 2 3 int main() 4 { 5 FILE *fp = NULL; 6 7 fp = fopen("/tmp/test.txt", "w+"); //第一个逗号前是文件位置。逗号之后是打开文件方式 8 fprintf(fp, "This is testing for fprintf...\n"); //逗号之前是一个指针,表明往里面输入。逗号之后fprintf是往文件里面输入 9 fputs("This is testing for fputs...\n", fp); 10 fclose(fp); //记得用完关闭文件 11 }
文件打开读取:
1 #include <stdio.h> 2 3 int main() 4 { 5 FILE *fp = NULL; 6 char buff[255]; 7 8 fp = fopen("/tmp/test.txt", "r"); 9 fscanf(fp, "%s", buff); //写入的时候和平常没有区别,还是只有字符串变量前不加‘&’,其他int、double等类型前都要加‘&’符号 10 printf("1: %s\n", buff ); 11 12 fgets(buff, 255, (FILE*)fp); //scanf遇到空格就会断开,gets会读取空格,遇到换行就结束 13 printf("2: %s\n", buff ); //255是限制最大读取内容长度 14 15 fgets(buff, 255, (FILE*)fp); 16 printf("3: %s\n", buff ); 17 fclose(fp); 18 19 }
文件读去和写入:
文件判断是否结尾要用feof()函数
1 #include <stdio.h> 2 int main() 3 { 4 FILE *fp = NULL; 5 double buff; 6 double s; 7 int w; 8 scanf("%lf",&s); 9 w=s; 10 fp = fopen("coursese.txt", "w"); 11 fprintf(fp,"%lf %lf %d",s,s,w); //这个%d后面不能加'\n',因为在文件中虽然一行什么东西都没有但是这一行确实存在,那么就不会 12 fclose(fp); //遇到文件结束标志。不仅换行不能交,空格也不能交 13 //即fprintf(fp,"%lf %lf %d ",s,s,w);、fprintf(fp,"%lf %lf %d ",s,s,w); 这两种形式都错 14 fp = fopen("coursese.txt", "r"); 15 while(1){ 16 if(feof(fp)) break; 17 fscanf(fp, "%lf%lf%d", &buff,&s,&w); 18 printf("%lf %lf %d\n",buff,s,w); 19 } 20 fclose(fp); 21 }
加上%s也可以:
1 #include <stdio.h> 2 int main() 3 { 4 FILE *fp = NULL; 5 double buff; 6 double s; 7 int w; 8 char ss[55]; 9 scanf("%lf",&s); 10 scanf("%s",ss); 11 w=s; 12 fp = fopen("coursese.txt", "w"); 13 fprintf(fp,"%lf %lf %d %s",s,s,w,ss); //这个%d后面不能加'\n',因为在文件中虽然一行什么东西都没有但是这一行确实存在,那么就不会 14 fclose(fp); //遇到文件结束标志。不仅换行不能交,空格也不能交 15 //即fprintf(fp,"%lf %lf %d ",s,s,w);、fprintf(fp,"%lf %lf %d ",s,s,w); 这两种形式都错 16 fp = fopen("coursese.txt", "r"); 17 while(1){ 18 if(feof(fp)) break; 19 fscanf(fp, "%lf%lf%d%s", &buff,&s,&w,ss); 20 printf("%lf %lf %d %s\n",buff,s,w,ss); 21 } 22 fclose(fp); 23 }
还有一种判断文件结束方式:fgetc()
但是这个函数相当于getchar(),它会在文件中吸取一个字符,这样的话文件指针就会向后移动一位,从而导致拿出来的时候数据和进去的时候不一样
代码:
1 #include <stdio.h> 2 int main() 3 { 4 FILE *fp = NULL; 5 double buff; 6 double s; 7 int w; 8 char ss[55]; 9 scanf("%lf",&s); 10 scanf("%s",ss); 11 w=s; 12 fp = fopen("coursese.txt", "w"); 13 fprintf(fp,"%lf %lf %d %s",s,s,w,ss); 14 fclose(fp); 15 fp = fopen("coursese.txt", "r"); 16 17 char ch; 18 while(1){ 19 ch=fgetc(fp); 20 if(ch==EOF) break; 21 fscanf(fp, "%lf%lf%d%s", &buff,&s,&w,ss); 22 printf("%lf %lf %d %s\n",buff,s,w,ss); 23 } 24 fclose(fp); 25 }
考虑到它判断文件的方式,我们可以输入的时候在每一条数据前面多加一个空格,来充当那个fgetc吸收的无用字符
代码:
1 #include <stdio.h> 2 int main() 3 { 4 FILE *fp = NULL; 5 double buff; 6 double s; 7 int w; 8 char ss[55]; 9 scanf("%lf",&s); 10 scanf("%s",ss); 11 w=s; 12 fp = fopen("coursese.txt", "w"); 13 fprintf(fp," %lf %lf %d %s",s,s,w,ss); //前面多加了一个空格。也可以加其他 14 fclose(fp); 15 fp = fopen("coursese.txt", "r"); 16 17 char ch; 18 while(1){ 19 ch=fgetc(fp); 20 if(ch==EOF) break; 21 fscanf(fp, "%lf%lf%d%s", &buff,&s,&w,ss); 22 printf("%lf %lf %d %s\n",buff,s,w,ss); 23 } 24 fclose(fp); 25 }
二进制读写可以看菜鸟教程