数据文件——之逐字符读取文件
2023-09-11 14:22:20 时间
一、不考虑文件打开失败,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 或其它命令继续
代码很简单,不做说明
相关文章
- C语言之文件操作07——读取文件数据并计算均值方差标准差
- 修改sqlserver2008默认的数据库文件保存路径
- C/C++ stat()函数:获取文件状态
- 补:小玩文件1-统计文本文件里的字符个数
- C#-文件只读时进行拷贝或删除报错, 对路径不允许访问
- 打开别人Xamarin项目找不到android.jar文件
- 统计一个文件中出现字符'a'的次数
- 使用 Linux 命令来获取输入文件中特定字符的出现次数
- 使用Matplotlib and GDAL对数字高程模型tif文件进行可视化
- dart系列之:元世界pubspec.yaml文件详解
- Android 从txt文件读数据生成曲线图
- linux问题——linux下误用rm删除文件解决方案
- Nacos配置的多文件加载与共享配置
- linux下vi编辑文件
- 【Python】:Python import导入上一级目录的文件和模块
- C#读取文件或者字符流的最后几行,类似linux的tail命令-OK
- 【Java I/O 流】文件字符输入输出流:FileReader 和 FileWriter