文件打開類型:
文件打開輸出就用:
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 }
二進制讀寫可以看菜鳥教程