|
查看: 1124|回复: 6
|
programming高手请帮帮忙。。。....
[复制链接]
|
|
|
C++ 为什么我run这个时,当我打eof 时 就会出现无限的?????出来的??要怎样解决阿??
#include <stdio.h>
int main()
{
int account;
char name[30],a;
float balance;
FILE *cfptr;
if((cfptr=fopen("clients.txt","w"))==NULL)
printf("file cant be opened");
else
{
printf("enter account,name and balance.\n");
printf("enter EOF to end input\n");
printf(":");
scanf("%d%s%f",&account,name,&balance);
while(!feof(stdin))
{
fprintf(cfptr,"%d%s%.2f\n",account,name,balance);
printf("?");
scanf("%d%s%f",&account,name,&balance);
}
fclose(cfptr);
}
system("PAUSE");
return 0;
} |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 13-10-2009 05:13 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 13-10-2009 05:49 PM
|
显示全部楼层
这题已经解决了。。。。现在是换这题了。。。
continue之前那题的。
不知道为什么run时就会出现无限的data。。。
#include<stdio.h>
int main ()
{
int account;
char name[30];
float balance;
FILE *cfptr;
if((cfptr=fopen("client.dat","r"))==NULL)
printf("file cant be opened");
else
{
printf("%-10s%-13s%s\n","account","name","balance");
fscanf(cfptr,"%d%s%f",&account,name,&balance);
while(!feof(cfptr))
{
printf("%-10d%-13s%7.2f\n",account,name,balance);
fscanf(cfptr,"%d%s%f",&account,name,&balance);
exit(-1);
}
fclose(cfptr);
}
system("PAUSE");
return 0;
}
帮帮忙。。。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 13-10-2009 06:46 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 13-10-2009 08:41 PM
|
显示全部楼层
大概看了你的程式,你的数据档案client.dat应该是Binary Mode 的数据,试用Notepad.exe打开来看看你这个档案, 如果看到有些 '乱码' (Non-Ascii-Code,就是不是普通人类的英文文字), 那就可以肯定了。你可以试试用Binary Mode 来打开档案, 如下。而且你的肯定你的数据档案 Data Layout 是符合你所定义的,就是 (int, char[30], float), 不然你的程式会无法读取数据或Crash。
还有,你的程式逻辑有些问题,看看下面:
#include<stdio.h>
int main ()
{
int account;
char name[30];
float balance;
FILE *cfptr;
if((cfptr=fopen("client.dat","rb"))==NULL)
printf("file cant be opened");
else
{
printf("%-10s%-13s%s\n","account","name","balance");
fscanf(cfptr,"%d%s%f",&account,name,&balance);
while(!feof(cfptr))
{
printf("%-10d%-13s%7.2f\n",account,name,balance);
fscanf(cfptr,"%d%s%f",&account,name,&balance);
exit(-1);
}
fclose(cfptr);
}
system("PAUSE");
return 0;
} |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 13-10-2009 08:52 PM
|
显示全部楼层
那放了exit(-1)后,电脑不就只能读到一个record以而已??
如果在client file里有很多record,不就display不出了?? |
|
|
|
|
|
|
|
|
|
|
发表于 13-10-2009 09:27 PM
|
显示全部楼层
|
首先,你知道exit(-1)是什么意思吗?你知道执行后程式会如何?先搞好你的程式可以读取一个record,再继续努力。 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|