1. 程式人生 > 實用技巧 >資料檔案——之讀取文字檔案中的整數

資料檔案——之讀取文字檔案中的整數

程式碼:

 1 //This is c program code!
 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 3   * 文件資訊: *** :~/WORKM/stutyCode/cCode/recipesProblemSolution/chapter06/test6_9.c
 4   * 版權宣告: *** :(魎魍魅魑)MIT
 5   * 聯絡信箱: *** :[email protected]
 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 }