2013-04-09 20 views
-1

我正在從文件(包含以換行符分隔的數字)讀取數據並計算金額。問題是,當讀取文件並在數組中存儲數量時,它會跳過第一個數量的第一個字母,例如: - 如果第一行有324,那麼它將讀取24,如果該行有3,那麼它會得到垃圾谷但其餘的線是好的。讀取文件的第一行不起作用

的代碼是

#include<graphics.h> 
#include<conio.h> 
#include<stdio.h> 
#include<stdlib.h> 
static int den[9]={1000,500,100,50,20,10,5,2,1}; 
void main() 
{ 
clrscr(); 
unsigned long long amt[9]; 
unsigned long long temp=0,total=0; 
int c=0,i=0,j=0,gd=DETECT,gm,x=66,y=22,font=8; 
fflush(stdin); 
FILE *fp; 
fp=fopen("OPENCASH.TXT","r"); 
while((c=getc(fp))!=EOF) 
{ 
    fscanf(fp,"%llu",&amt[i]); 
    i++; 
} 
initgraph(&gd,&gm,"C:\\TC\\BGI"); 
settextstyle(font,HORIZ_DIR,1); 
printf(" \t  "); 
for (i=0;j<50;j++) 
{ 
    printf("_"); 
} 
outtextxy(x, y, " The Closing Cash Denomination"); 
printf("\n\n\n\t "); 
for (i=0;i<50;i++) 
{ 
    printf("_"); 
} 
font=5; 
settextstyle(font,HORIZ_DIR,1); 
x=30; 
y=87; 
outtextxy(x,y,"1000"); 
font=6; 
settextstyle(font,HORIZ_DIR,1); 
outtextxy(104,y,"X"); 
gotoxy(20,7); 
printf("%llu",amt[0]); 
outtextxy(205,--y,"="); 
temp=den[0]*amt[0]; 
total=total+temp; 
gotoxy(35,7); 
printf("%llu",temp); 
++y; 
font=5; 
x=42; 
y=y+34; 
outtextxy(x,y,"500"); 
font=6; 
settextstyle(font,HORIZ_DIR,1); 
outtextxy(104,y,"X"); 
gotoxy(20,9); 
printf("%llu",amt[1]); 
outtextxy(205,--y,"="); 
temp=den[1]*amt[1]; 
total=total+temp; 
gotoxy(35,9); 
printf("%llu",temp); 
++y; 
font=5; 
y=y+32; 
outtextxy(x,y,"100"); 
font=6; 
settextstyle(font,HORIZ_DIR,1); 
outtextxy(104,y,"X"); 
gotoxy(20,11); 
printf("%llu",amt[2]); 
outtextxy(205,--y,"="); 
temp=den[2]*amt[2]; 
total=total+temp; 
gotoxy(35,11); 
printf("%llu",temp); 
++y; 
font=5; 
x=54; 
y=y+31; 
outtextxy(x,y,"50"); 
font=6; 
settextstyle(font,HORIZ_DIR,1); 
outtextxy(104,y,"X"); 
gotoxy(20,13); 
printf("%llu",amt[3]); 
outtextxy(205,--y,"="); 
temp=den[3]*amt[3]; 
total=total+temp; 
gotoxy(35,13); 
printf("%llu",temp); 
++y; 
font=5; 
y=y+31; 
outtextxy(x,y,"20"); 
font=6; 
settextstyle(font,HORIZ_DIR,1); 
outtextxy(104,y,"X"); 
gotoxy(20,15); 
printf("%llu",amt[4]); 
outtextxy(205,--y,"="); 
temp=den[4]*amt[4]; 
total=total+temp; 
gotoxy(35,15); 
printf("%llu",temp); 
++y; 
font=5; 
y=y+34; 
outtextxy(x,y,"10"); 
font=6; 
settextstyle(font,HORIZ_DIR,1); 
outtextxy(104,y,"X"); 
gotoxy(20,17); 
printf("%llu",amt[5]); 
outtextxy(205,--y,"="); 
temp=den[5]*amt[5]; 
total=total+temp; 
gotoxy(35,17); 
printf("%llu",temp); 
++y; 
font=5; 
x=64; 
y=y+32; 
outtextxy(x,y,"5"); 
font=6; 
settextstyle(font,HORIZ_DIR,1); 
outtextxy(104,y,"X"); 
gotoxy(20,19); 
printf("%llu",amt[6]); 
outtextxy(205,--y,"="); 
temp=den[6]*amt[6]; 
total=total+temp; 
gotoxy(35,19); 
printf("%llu",temp); 
++y; 
font=5; 
y=y+31; 
outtextxy(x,y,"2"); 
font=6; 
settextstyle(font,HORIZ_DIR,1); 
outtextxy(104,y,"X"); 
gotoxy(20,21); 
printf("%llu",amt[7]); 
outtextxy(205,--y,"="); 
temp=den[7]*amt[7]; 
total=total+temp; 
gotoxy(35,21); 
printf("%llu",temp); 
++y; 
font=5; 
y=y+31; 
outtextxy(x,y,"1"); 
font=6; 
settextstyle(font,HORIZ_DIR,1); 
outtextxy(104,y,"X"); 
gotoxy(20,23); 
printf("%llu",amt[8]); 
outtextxy(205,--y,"="); 
temp=den[8]*amt[8]; 
total=total+temp; 
gotoxy(35,23); 
printf("%llu",temp); 
y=y+9; 
outtextxy(264,y,"___________"); 
y=y+27; 
outtextxy(150,y,"Total Cash"); 
gotoxy(35,25); 
printf("%llu",total); 
getch(); 
} 
+1

您正在使用此行'while((c = getc(fp))!= EOF)'消耗第一個字符。你需要改變它。 :) – Bechir 2013-04-09 15:09:23

+0

hoo我的!它是不是Borland?嘆! – 2013-04-09 15:11:34

回答

0

的第一個字符是由getc功能而被消耗。

你可以改變你的閱讀循環如下:

while(!feof(fp)) { 
    fscanf(fp,"%llu",&amt[i]); 
    i++; 
} 
+0

不使用feof更好也更安全,但直接檢查fscanf的結果。 – Lundin 2013-04-09 15:17:53

1

while循環是錯誤的,

while((c=getc(fp))!=EOF) 
{ 
    fscanf(fp,"%llu",&amt[i]); 
    i++; 
} 

getc()消耗字符,

測試如下,

while(fscanf(fp,"%llu",&amt[i]) > 0) 
{ 
    i++; 
} 

您也可以使用功能ungetc()取消getc()的影響。

1

這段代碼有許多嚴重的問題。

明顯的錯誤:

  • while循環,正如其他人已經指出。將其替換爲fscanf的結果檢查。請勿使用feof
  • fflush不能用於stdin或任何其他輸入流。這會調用未定義的行爲,並且程序可能會以意想不到的方式崩潰或行爲。
  • 您的程序只適用於安裝了編譯器的系統。您需要將BGI庫目標文件添加到您的項目中。
  • 您不檢查文件是否成功打開。
  • 您不要撥打電子郵件fclose,這樣您的程序就會造成資源泄漏並不必要地讓文件保持打開狀態。

非標準胡言亂語:

  • unsigned long long不是由C99標準之前的C語言,你的廢話編譯器不支持支持。無論long long對您的編譯器意味着什麼,它可能是不可移植的。同樣,%ll格式說明符沒有爲您的編譯器定義好。
  • 由於這是針對託管系統(DOS)的,因此您必須將main聲明爲int main (void),否則您的代碼將無法在符合的C編譯器上編譯。
  • 因爲main始終返回0,所以不能在託管系統上省略return 0語句。這個規則在C99中已經放寬了,但由於你的編譯器不符合C99標準(或者實際上任何C標準),所以你必須始終有return 0

其他問題:

  • 不要使用 「幻數」 所有的地方,使用的常量。 const int或#defines。
  • 您的注意事項很晦澀難懂。 2或4個空格是慣例。
  • 您必須使用功能和模塊化編程。這只是一個難以理解的大混亂。
  • 你永遠不會調用closegraph()。無論是否造成資源泄漏,我都不確定。
  • 在同一個屏幕上混合使用printf和outtextxy看起來像垃圾。但是,再次,這是DOS的,所以可能是垃圾是理想的。
+0

我會嘗試所有上述缺陷來正確實施。 ** AND ** 請幫我安裝或插入** BGI庫文件到您的項目** – 2013-04-10 03:03:17

+0

@VipulSonker從我記得,你創建一個新的項目,然後從Turbo C中添加一個文件名爲'egavga.bgi'的庫。它是可以與您的項目鏈接在一起的各種目標文件。如果我沒有記錯,可以將其複製到您的項目文件夾中。 – Lundin 2013-04-10 06:22:01

相關問題