2013-04-22 124 views
-2

我有客戶數據,如客戶號碼,位置座標等,並且文本文件中有25個客戶。C編程:從文件中讀取

這是我的代碼。當我打印時,這給了我一個零輸出。

#include <stdio.h> 
#include <conio.h> 
#include <string.h> 
#include <stdlib.h> 
#define customerCount 25 


struct customerData 
{ 
     int customerNo; 
     double xCoordinate; 
     double yCoordinate; 
     double demand; 
     double readyTime; 
     double dueTime; 
     double serviceTime; 
}; 

int main() 
{ 
    int i; 
    struct customerData allSubscriber[customerCount]; 
    FILE *dosya; 
    dosya = fopen("c:\\solomon_c101.txt", "r"); 

     for(i=1; i<=customerCount; i++) 
     { 
       fscanf(dosya, "%d %f %f %f %f %f %f", &allSubscriber[i].customerNo, &allSubscriber[i].xCoordinate, &allSubscriber[i].yCoordinate, &allSubscriber[i].demand, &allSubscriber[i].readyTime, &allSubscriber[i].dueTime, &allSubscriber[i].serviceTime); 
     } 
     fclose(dosya); 

    for(i=1; i<=customerCount; i++) 
    { 
      printf("%f\n", &allSubscriber[i].xCoordinate); 
    } 


    getch(); 
    return 0; 
}   
+0

不要使用'conio。 h',它不是標準的C,並且可能不適用於所有環境。 – 2013-04-22 18:54:21

+0

你想用'%lf'和'fscanf()',而不是'%f'。 – FatalError 2013-04-22 18:55:22

回答

0

使用此:

printf("%f\n", allSubscriber[i].xCoordinate); 

代替

printf("%f\n", &allSubscriber[i].xCoordinate); 

至於另一網友指出,在fscanf()。在printf的使用%lf作爲格式說明符double()它被提升加倍。

不要使用conio.hgetch()。那些都不是標準C.

+0

'%lf'只用於'scanf()'。對於'printf()''%f'打印'double'(當作爲變量傳遞時''float'被提升爲'double')。 – FatalError 2013-04-22 19:03:25

+0

@FatalError採取點。感謝您指出。 – 2013-04-22 19:04:40

+0

謝謝,但是當我嘗試這個時,它會在輸出中給我提供不相關的數字。我用getch()停止關閉控制檯窗口。如果我刪除conio.h和getch()並使用getchar(),那麼可以嗎? – 2013-04-22 19:13:52

0

使用%lf讀取在doublescanf

printf("%f\n", &allSubscriber[i].xCoordinate); //刪除&