我正在读取一个文件(包含换行符分隔的数字)并计算金额。问题是在读取文件并将数量存储在数组中时,它正在跳过第一个数量的第一个字母,例如: - 如果第一行有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();
}
答案 0 :(得分: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 :(得分:1)
此代码存在许多严重问题。
明显的错误:
fscanf
的结果。请勿使用feof
。fclose
,因此您的程序会造成资源泄漏并且不必要地保持文件打开。非标准的乱码:
unsigned long long
,您的垃圾编译器不支持。无论long long
对编译器意味着什么,它都可能是不可移植的。同样,%ll
格式说明符没有为您的编译器定义良好。int main (void)
,否则您的代码将无法在符合标准的C编译器上进行编译。return 0
语句。在C99中放宽了这条规则,但由于您的编译器不符合C99标准(或任何C标准),因此最后必须始终有return 0
。 其他问题:
const int
或#defines。答案 2 :(得分:0)
第一个字符由getc
函数使用。
您可以按如下方式更改阅读循环:
while(!feof(fp)) {
fscanf(fp,"%llu",&amt[i]);
i++;
}