在一个文件末尾增加字符串,并在控制台打印出来

 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
";
 9 
10     fopen_s(&fp_write,"E:\first.txt","a+");
11     if(fp_write == NULL)
12     {
13         printf("Can't open the file!
");
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!
");
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 }
原文地址:https://www.cnblogs.com/cpsmile/p/4776877.html