[英]When reading input from a file, my C program is not reading the integers correctly. Any suggestions?
我有一個文本文件,應該使用文件輸入/輸出技術來閱讀:
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。
感謝您的幫助。
您正在打印變量的地址,而不是它的值。
刪除地址: printf("%d", CA1);
另一件事,您沒有檢查,如果文件打開成功,則應該進行處理,尤其是在某些情況下無法正常工作時。
if(!input)
{
printf("Could not open the file specified.");
return -1;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.