数据文件——之写入并读取该文件

代码:

 1 //This is c program code!
 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 3   * 文档信息: *** :~/WORKM/stutyCode/cCode/recipesProblemSolution/chapter06/test6_19.c
 4   * 版权声明: *** :(魎魍魅魑)MIT
 5   * 联络信箱: *** :guochaoxxl@163.com
 6   * 创建时间: *** :2020年11月22日的上午10:44
 7   * 文档用途: *** :数据结构与算法分析-c语言描述
 8   * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl)
 9   * 修订时间: *** :2020年第46周 11月22日 星期日 上午10:44 (第327天)
10   * 文件描述: *** :自行添加
11  * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/
12 #include <stdio.h>
13 
14 void closeState(FILE *fPtr){                                                            
15     int clo = fclose(fPtr);
16     if(clo == -1){
17         puts("file-closing failed.");
18     }
19     if(clo == 0){
20         puts("file-closing successfully.");
21     }
22 }
23 
24 int main(int argc, char **argv)
25 {   
26     char store[80];
27     FILE *fPtr = fopen("pune.txt", "w+");
28     if(fPtr != NULL){
29         puts("File pune.txt opened successfully.");
30         fputs("Jiujiang is a very nice city.\n", fPtr);
31         fputs("I like it very much.\n", fPtr);
32         fputs("Welcom to Lushan of Jiujiang.\n", fPtr);
33         puts("Text written to file pune.txt successfully.");
34         rewind(fPtr);
35         char *cPtr = fgets(store, 80, fPtr);
36         while(cPtr != NULL){
37             printf("%s", store);
38             cPtr = fgets(store, 80, fPtr);
39         }
40         puts("Contents of file pune.txt: ");
41         puts(store);
42     
43         closeState(fPtr);
44     }else{
45         puts("File-opening failed!");
46     }
47     
48     return 0;
49 }

 

posted @ 2020-11-28 21:38  叕叒双又  阅读(157)  评论(0编辑  收藏  举报