急,fread()查不出来
下面的程序清单:
void Data_insert() /*添加数据*/
{
FILE *fp;
List List_money;
ClrScr();
fp=fopen( "noname ", "rb ");/*读方式打开文件*/
if(fp==NULL)
fp=fopen( "noname.dat ", "wb "); /*写方式打开文件*/
else/*如果有书就添加*/
{
fclose(fp);
fp=fopen( "noname.dat ", "ab ");/*追加方式打开文件*/
}
ClrScr();
gotoxy(2,3);
printf( "Please input the NO. of the record\t ");
scanf( "%d ",&List_money.num);
gotoxy(2,4);
printf( "Please input your income this time\t ");
scanf( "%f ",&List_money.income);
gotoxy(2,5);
printf( "Please input your pay this time\t ");
scanf( "%f%*c ",&List_money.pay);
gotoxy(2,6);
if(fwrite(&List_money,sizeof(List),1,fp)!=1)
printf( "file write error ");
else
printf( "the data you have input was saved! ");
getch();
ClrScr();
}
/*查询会员*/
void MemberConsult()
{
FILE *fp;
List List_money;
int flag=0,temp_num;
ClrScr();
gotoxy(2,3);
printf( "Input the NO.:\t ");/*输入要查询的代码*/
scanf( "%d ",&temp_num);
fp=fopen( "noname.dat ", "rb ");
if(fp==NULL)/*没找到文件*/
{
ClrScr();
gotoxy(2,3);
printf( "Maybe you don 't have opened any file! ");
fclose(fp);
gotoxy(2,4);
printf( "Please input the file name!\t ");
scanf( "%s ",FILENAME);
strcat(FILENAME, ".dat ");
if((fp=fopen( "noname.dat ", "rb "))==NULL)
{
gotoxy(2,5);
printf( "the system can 't open the file ");
gotoxy(2,6);
printf( "Please run other operation! ");
getch();
ClrScr();
return;
}
}
while(!feof(fp))/*查询资料*/
{
ClrScr();
if((fread(&List_money,sizeof(List),1,fp))!=1)
{ gotoxy(2,3);
printf( "file read error! ");
getch();
break; }
if(List_money.num==temp_num)/*比较*/
{
ClrScr();
gotoxy(2,3);
printf( "NO.:%d ",List_money.num);
gotoxy(2,4);
printf( "income:%f ",List_money.income);
gotoxy(2,5);
printf( "pay:%f ",List_money.pay);
flag=1;
break;
}
}
if(flag==0)
{
ClrScr();
gotoxy(2,3);
printf( "No found the file or data! ");
getch();
ClrScr();
}
fclose(fp);
}
程序清单是想先输入数据给硬盘再输出来,但老是在fread()那出错!!!老是显示file read error!错在哪,急呀!
[解决办法]
fread()函数的返回值是实际读取的字节数,你读取的是(sizeof(List)*1)个字节,返回值当然不是1了,所以会进入if语句里,显示file read error!
[解决办法]
fread()函数的返回值是实际读取的字节数?
不是吧?
------解决方案--------------------
fread(buffer,size,count,fp)
返回的是count吧?
[解决办法]
回复人:june_t() ( 一级(初级)) 信誉:100 2007-7-27 21:33:03 得分:0
?
fread()函数的返回值是实际读取的字节数?
不是吧?
Top
回复人:june_t() ( 一级(初级)) 信誉:100 2007-7-27 21:34:43 得分:0
?
fread(buffer,size,count,fp)
返回的是count吧?
-------------------------------------------
也不是返回读取的字节数,也不是返回的count数,是返回实际读取数据的 "项数 ".
RETURN VALUE
fread and fwrite return the number of items successfully read or written (i.e., not the number of characters). If
an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).