1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main() 5 { 6 FILE *fp_write, *fp_read; 7 char ch; 8 char add[] = "An extra line\n"; 9 10 fopen_s(&fp_write,"E:\\first.txt","a+"); 11 if(fp_write == NULL) 12 { 13 printf("Can‘t open the file!\n"); 14 system("pause"); 15 return 0; 16 } 17 //定位到文件末尾 18 fseek(fp_write,0,SEEK_END); 19 fputs(add,fp_write); 20 //fwrite(add,sizeof(add),1,fp_write); 21 //先关闭写入流再打开读出流 22 fclose(fp_write); 23 24 fopen_s(&fp_read,"E:\\first.txt","r"); 25 if(fp_read == NULL) 26 { 27 printf("fp_read,Can‘t open the file!\n"); 28 system("pause"); 29 return 0; 30 } 31 while((ch = fgetc(fp_read)) != EOF) 32 putchar(ch); 33 34 fclose(fp_read); 35 36 system("pause"); 37 return 0; 38 }
时间: 2024-10-25 06:01:23