2015-11-12 27 views
2
int main() { 
    int i;int j;char c;double num; 
    double point[65][2]; 
    FILE *myfile; 
    myfile = fopen("point.txt","r"); 
    if(myfile == NULL) 
    { 
     printf("can not open file\n"); 
    } 
    else{ 
     for(i=0;i<65;i++){ 
      for (j=0; j<2; j++) { 
       fscanf(myfile, "%lf", &num); 
       point[i][j]=num; 
       printf("%lf",point[i][j]); 

      } 
     } 
    } 
    fclose(myfile); 
} 

我不知道爲什麼它總是給我一個空數組。數據中有65個觀測值。這就是爲什麼我創建一個65x2陣列。讀取2維數據文件並將它們存儲到數組中(這是我第一次使用c)

文件看起來是這樣的:

1.87046225914495 0.37205807606083

1.51453361512525 0.45942874936008

..

..

+0

由hexasoft建議的檢查條件,修改後的代碼應檢查'fscanf'的'return'。並且也請顯示文件的格式的位。 – ameyCU

+0

你可以發佈你的point.txt嗎? – danielfranca

+0

我剛將65改爲14行文件,效果很好。我的point.txt文件:http://pastebin.com/NqtU0a3a – danielfranca

回答

0

我看不到這個代碼的任何錯誤。它可以任何改變一點點:

  • 可以直接調用fscanf到目標值(fscanf(myfile, "%lf", &(point[i][j]));
  • 如果你的文件格式將永遠是X * 2點的值,可以刪除第二個for環和閱讀兩值(fscanf(myfile, "%lf%lf", &(point[i][0]), &(point[i][1]));
  • 你應該閱讀後打印表格內容,讓你一定要打印最終值(如果,讓說,你覆蓋在一個點的一些值)

我唯一GUE ss會與文件格式有關。如果scanf未能讀取值(沒有足夠的值,格式錯誤的文件...),您將無法獲得結果。

如@ameyCU所述,您必須檢查返回值fscanf。它返回成功讀取的「令牌」的數量。你應該檢查它是真的== 1,否則它意味着格式不好(或文件中沒有可用的數據)。在這種情況下,部分或全部表格的內容未定義(並且取決於操作系統可以填充0)。

編輯:如果你真的在這個文件中有65x2的值檢查無效字符,即如果你使用Word或類似的創建數據文件。

0

這是您的問題的解決方案,嘗試閱讀和理解代碼。基本上,這個想法是正確的,但我糾正了你犯錯誤的地方。解決方案的主要工作是在while()循環中。所以通過它。

#include<stdio.h> 
//#pragma warning(disable : 4996) 


int main() 
{ 
     int i=0, in = 0; 
     int j=0, jn = 0; 
     int numberOfCol = 0; 

     double num1,num2; 
     double point[65][2]; 
     FILE *myfile=NULL; 

     myfile = fopen("test.txt", "r"); 
     if (myfile == NULL) 
     { 
      printf("can not open file\n"); 
     } 
     else 
     { 
      while (!feof(myfile)) 
      { 
       fscanf(myfile, "%lf %lf", &num1, &num2);//since your file contains two numbers per row you need two variables to store the values 
       point[i][j] = num1;//At this point (i=0,j=0) you will get the first value of in the row/line. 
       printf("%lf ", point[i][j]); 
       ++j;//incremet j so that you move the index to next column i.e (i=0,j=1) 
       point[i][j] = num2;//store the second column value of the row/line 
       numberOfCol++; 
       printf("%lf", point[i][j]); 
       printf("\n"); 
       ++i;//go to next row/line 
       j = 0;//set back the column index to 0 aka the first column index    
      }//Repeat this process till you reach End of file 
      } 

    fclose(myfile); 
    printf("File Data..\n"); 
    for (in = 0; in<i; in++) 
    { 
     for (jn = 0; jn<numberOfCol; jn++) 
     { 
      printf("%lf ",point[in][jn]); 
     } 
     printf("\n"); 
    } 

    return 0; 
} 

編輯:這是一個由在comments.Thanks Hexasoft

#include<stdio.h> 
//#pragma warning(disable : 4996) 


int main() 
{ 
    int i=0, in = 0; 
    int j=0, jn = 0; 
    int numberOfCol = 0; 

    double num1,num2; 
    double point[65][2]; 
    int result; 
    FILE *myfile=NULL; 

    myfile = fopen("test.txt", "r"); 
    if (myfile == NULL) 
    { 
     printf("can not open file\n"); 
    } 
    else 
    { 

     while (i<65)//65 or how may ever rows/lines of data you have in your file 
     { 
      result=fscanf(myfile, "%lf %lf", &num1, &num2); 

      if (result == 2) 
      { 
       point[i][j] = num1; 
       printf("%lf ", point[i][j]); 
       ++j; 
       numberOfCol = j+1; 

       point[i][j] = num2; 
       printf("%lf", point[i][j]); 
       printf("\n"); 
       ++i; 
       j = 0; 
      } 
      else 
      { 
       printf("Format Error!\n"); 
      } 

      if (feof(myfile)) 
      { 
       printf("End of file reached!\n"); 
       break; 
      } 

    } 

} 

fclose(myfile); 
printf("File Data..\n"); 
for (in = 0; in<i; in++) 
{ 
    for (jn = 0; jn<numberOfCol; jn++) 
    { 
     printf("%lf ",point[in][jn]); 
    } 
    printf("\n"); 
} 

    return 0; 
} 
+0

嗯,我無法找到你找到「解決問題的方法」。你的'while'很好,但如果輸入文件中有足夠的數據,它不會改變任何東西(與for循環相比)。更糟糕的是,如果超過65x2的數據存在,您將在'point'結束後插入數據(所以UB)。並且檢查「feof」是不夠的,因爲它在讀取失敗後被提升。因爲你不檢查'fscanf'返回你將會有未定義的值。 – hexasoft

+0

@hexasoft感謝您對檢查條件的建議。我添加了它們。 – Mutex202

相關問題