数据文件——之逐字符读取文件

一、不考虑文件打开失败,test6_1.c

 1 //This is c program code!
 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 3   * 文档信息: *** :~/WORKM/stutyCode/cCode/recipesProblemSolution/chapter06/test6_1.c
 4   * 版权声明: *** :(魎魍魅魑)MIT
 5   * 联络信箱: *** :guochaoxxl@163.com
 6   * 创建时间: *** :2020年11月20日的下午04:57
 7   * 文档用途: *** :数据结构与算法分析-c语言描述
 8   * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl)
 9   * 修订时间: *** *2020年第46周 11月20日 星期五 下午05:00 (325天)
10   * 文件描述: *** :自行添加
11  * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/
12 #include <stdio.h>
13 
14 int main(int argc, char **argv)
15 {
16         FILE *fPtr = fopen("test6_1.c", "r");
17         int num = fgetc(fPtr);
18 
19         while(num != EOF){
20                 putchar(num);
21                 num = fgetc(fPtr);
22         }
23 
24         fclose(fPtr);
25 
26         return 0;
27 }

运行结果:

 1 请按 ENTER 或其它命令继续
 2 //This is c program code!
 3 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 4   * 文档信息: *** :~/WORKM/stutyCode/cCode/recipesProblemSolution/chapter06/test6_1.c
 5   * 版权声明: *** :(魎魍魅魑)MIT
 6   * 联络信箱: *** :guochaoxxl@163.com
 7   * 创建时间: *** :2020年11月20日的下午04:57
 8   * 文档用途: *** :数据结构与算法分析-c语言描述
 9   * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl)
10   * 修订时间: *** *2020年第46周 11月20日 星期五 下午05:00 (325天)
11   * 文件描述: *** :自行添加
12  * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/
13 #include <stdio.h>
14 
15 int main(int argc, char **argv)
16 {
17         FILE *fPtr = fopen("test6_1.c", "r");
18         int num = fgetc(fPtr);
19 
20         while(num != EOF){
21                 putchar(num);
22                 num = fgetc(fPtr);
23         }
24 
25         fclose(fPtr);
26 
27         return 0;
28 }
29 
30 请按 ENTER 或其它命令继续

二、考虑了文件打开和关闭失败的情形test6_2.c

 1 //This is c program code!
 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 3   * 文档信息: *** :~/WORKM/stutyCode/cCode/recipesProblemSolution/chapter06/test6_2.c
 4   * 版权声明: *** :(魎魍魅魑)MIT
 5   * 联络信箱: *** :guochaoxxl@163.com
 6   * 创建时间: *** :2020年11月20日的下午05:04
 7   * 文档用途: *** :数据结构与算法分析-c语言描述
 8   * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl)
 9   * 修订时间: *** :2020年第46周 11月20日 星期五 下午05:04 (第325天)
10   * 文件描述: *** :自行添加
11  * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/
12 #include <stdio.h>
13 
14 int main(int argc, char **argv)
15 {
16         FILE *fPtr = fopen("test6_2.c", "r");
17         if(fPtr != NULL){
18                 puts("File test6_2.c is opened successfully!");
19                 puts("Contents of file test6_2.c: ");
20                 int num = fgetc(fPtr);
21 
22                 while(num != EOF){
23                         putchar(num);
24                         num = fgetc(fPtr);
25                 }
26                 int mun = fclose(fPtr);
27                 if(mun == -1){
28                         puts("File closing failed!");
29                 }else{
30                         puts("File closed successfully!");
31                 }
32         }else{
33                 puts("File opened failed!");
34         }
35 
36         return 0;
37 }

运行结果:

 1 请按 ENTER 或其它命令继续
 2 File test6_2.c is opened successfully!
 3 Contents of file test6_2.c: 
 4 //This is c program code!
 5 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 6   * 文档信息: *** :~/WORKM/stutyCode/cCode/recipesProblemSolution/chapter06/test6_2.c
 7   * 版权声明: *** :(魎魍魅魑)MIT
 8   * 联络信箱: *** :guochaoxxl@163.com
 9   * 创建时间: *** :2020年11月20日的下午05:04
10   * 文档用途: *** :数据结构与算法分析-c语言描述
11   * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl)
12   * 修订时间: *** :2020年第46周 11月20日 星期五 下午05:04 (第325天)
13   * 文件描述: *** :自行添加
14  * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/
15 #include <stdio.h>
16 
17 int main(int argc, char **argv)
18 {
19         FILE *fPtr = fopen("test6_2.c", "r");
20         if(fPtr != NULL){
21                 puts("File test6_2.c is opened successfully!");
22                 puts("Contents of file test6_2.c: ");
23                 int num = fgetc(fPtr);
24 
25                 while(num != EOF){
26                         putchar(num);
27                         num = fgetc(fPtr);
28                 }
29                 int mun = fclose(fPtr);
30                 if(mun == -1){
31                         puts("File closing failed!");
32                 }else{
33                         puts("File closed successfully!");
34                 }
35         }else{
36                 puts("File opened failed!");
37         }
38 
39         return 0;
40 }
41 File closed successfully!
42 
43 请按 ENTER 或其它命令继续

代码很简单,不做说明

原文地址:https://www.cnblogs.com/guochaoxxl/p/14012011.html