我用c编了一个读文件的程序
#include<stdio.h>
#include<string.h>
void main()
{
FILE *fp;
char acount[14+1];
char f_acount[14+1];
char f_date[8+1];
char year[4+1];
char month[2+1];
char day[2+1];
char f_time[6+1];
char hour[2+1];
char sec[2+1];
char mini[2+1];
char f_type;
char f_count[10+1];
float count;
int i,cnt;
acount[14]=0;
f_acount[14]=0;
f_date[8]=0;
f_time[6]=0;
f_count[10]=0;
year[4]=0;
month[2]=0;
day[2]=0;
hour[2]=0;
sec[2]=0;
mini[2]=0;
count=0.0;
printf("input your Acount:\n");
for(i = 0;i < 14;i++)
{
acount[i]=getchar();
}
printf("\n");
if((fp=fopen("../T/abc","r"))==NULL)
{
printf("file open erro!\n");
return;
}
while(1)
{
for(cnt = 0;cnt < 14; cnt++)
{
f_acount[cnt] = fgetc(fp);
}
if(!strcmp(acount,f_acount))
{
for(cnt = 0;cnt < 8;cnt++)
{
f_date[cnt] = fgetc(fp);
}
for(cnt = 0;cnt<8;cnt++)
{
if(cnt < 4)
{
year[cnt]=f_date[cnt];
}
else if(cnt < 6)
{
month[cnt-4]=f_date[cnt];
}
else day[cnt-6]=f_date[cnt];
}
for(cnt = 0;cnt < 6;cnt++)
{
f_time[cnt]=fgetc(fp);
}
for(cnt = 0;cnt < 6;cnt++)
{
if(cnt < 2)
{
hour[cnt]=f_time[cnt];
}
else if(cnt<4)
{
sec[cnt-2]=f_time[cnt];
}
else mini[cnt-4]=f_time[cnt];
}
f_type = fgetc(fp);
for(cnt = 0;cnt < 10;cnt++)
{
f_count[cnt] = fgetc(fp);
}
for(cnt = 0;cnt < 10;cnt++)
{
count=count*10+f_count[cnt]-48;
}
printf("编号: %-14s\n",f_acount);
printf("日期: %-4s 年 %-2s 月 %-2s 日\n",year,month,day);
printf("时间: %-2s : %-2s : %-2s\n",hour,sec,mini);
if(f_type == '1') 
{
printf("类型:a\n");
}
else
{
printf("类型:b\n");
}
printf("is: %10.2f\n",count/100);
fclose(fp);
return;
}
fseek(fp,23,1);
fgetc(fp);/////////问题在这,为什么用fget()可以用fseek(fp,1,1 )
                            /////////就不行,试了好几次都不行
if(!feof(fp)) 
{
fseek(fp,1,1);
}
else
{
break;
};
}
printf("there is no this acount!\n");
fclose(fp);
return;
}
是不是fseek函数的指针指向当前位置啊 还是feof函数用的不对,请高手指点在下,不胜感激

解决方案 »

  1.   

    你仔细看一下feof的使用方法,
    Return Value
    The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file. There is no error return.它必须和read 操作结合起来用,不然无效的。
      

  2.   

    fseek
    Moves the file pointer to a specified location.int fseek( FILE *stream, long offset, int origin );
    Return ValueIf successful, fseek returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined.
    feof
    Tests for end-of-file on a stream.int feof( FILE *stream );
    Return ValueThe feof function returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file. There is no error return.
    fseek用于定位要读的起始位置。
    feof 用于判断是否读到了文件末尾。
      

  3.   

    这样的话是不是说,我用fseek函数只是将指针移动到了文件尾端,并没有读所以feof就不起作用,直到我试图读的时候才引出了返回值-1,这个时候feof才起作用。这样就明白了,谢谢大虾的指点,小弟感激不尽
      

  4.   

    这样的话是不是说,我用fseek函数只是将指针移动到了文件尾端,并没有读所以feof就不起作用,直到我试图读的时候才引出了返回值-1,这个时候feof才起作用。这样就明白了,谢谢大虾的指点,小弟感激不尽
    ========
    就是这样,必须读才有效