2015-10-19 55 views
1

我已經得到了我應該用文件輸入/輸出技術來讀取文本文件:從文件讀取輸入時,我的C程序沒有正確讀取整數。有什麼建議麼?

47900  31007 
34500  9100 
57984  14822 

這裏是我的代碼:

#include <stdio.h> 

FILE *input; 
int CA1; 
int CA2; 
int CA3; 
int CL1; 
int CL2; 
int CL3; 


int main (void) 
{ 
    input = fopen("c:\\class\\currentratio.txt","r"); 
    fscanf(input,"%d %d\n", &CA1, &CL1); 
    fscanf(input, "%d %d\n", &CA2, &CL2); 
    fscanf(input, "%d %d\n", &CA3, &CL3); 
    printf("%d", &CA1); 
    fclose(input); 
    return 0; 
} 

當我打印的數字,他們是

4229680,4229704,4229744,4229712,4229664和4229720.

感謝您的幫助。

+1

您實際使用哪種語言?標記,而不是其他一些語言。 –

+0

用'printf(「%d」,&CA1);''你試圖打印變量'CA1'的地址,而不是值,嘗試刪除'&'字符;) – vagoberto

+0

也試着編譯你的代碼像'-Wall'這樣的標誌顯示警告信息(如果有的話)。例如,你的代碼編譯的格式爲:'code.c:17:3:warning:format'%d'需要類型爲'int'的參數,但參數2的類型爲'int *'[-Wformat =] printf (「%d」,&CA1)' – vagoberto

回答

6

您正在打印變量的地址而不是數值。
擺脫地址:printf("%d", CA1);

另一件事,你不檢查,如果文件打開成功,你應該處理這尤其是在一些情況下無法正常工作。

if(!input) 
{ 
    printf("Could not open the file specified."); 
    return -1; 
}