数据文件——之读取文本文件中的整数

代码:

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

 

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