
C语言中关于fseek函数的疑问
我在一个函数中两次调用了fseek函数,都是fseek(fp,49L,0);但如果把第一个fseek中的49L改成其他数就可以运行,如果把第二个fseek中的49L换成其...
我在一个函数中两次调用了fseek函数,都是fseek(fp,49L,0);但如果把第一个fseek中的49L改成其他数就可以运行,如果把第二个fseek中的49L换成其他数就有读取错误了,如下:
void loadfromfile(struct stu *a,struct xibu2 *r,int n,int k)
{
int h;
FILE* fp;
FILE* fp1;
system("cls");
if((fp=fopen("studentinfo.txt","r"))!=NULL)
fseek(fp,49L,0);
while(!feof(fp))
{
fscanf(fp,"%s\t",a[m].num);
fscanf(fp,"%s\t",a[m].name);
fscanf(fp,"%c\t",&a[m].sex);
fscanf(fp,"%d\t",&a[m].age);
fscanf(fp,"%s\t",a[m].xibu);
fscanf(fp,"%s\t",a[m].zhuanye);
fscanf(fp,"%s\t",a[m].classs);
fscanf(fp,"%f\t",&a[m].score[0]);
fscanf(fp,"%f\t",&a[m].score[1]);
fscanf(fp,"%f\t",&a[m].score[2]);
m++;
}
fclose(fp);
if((fp1=fopen("xibu.txt","r"))!=NULL)
fseek(fp1,21L,0);
while(!feof(fp1))
{
fscanf(fp1,"%s\t",r[f].name);
fscanf(fp1,"%d\t",bj[f]);
for(h=0;h<bj[f];h++)
{
fscanf(fp1,"%s\t",r[f].banji[h]);
}
f++;
}
fclose(fp1);
printf("读取文件数据成功!\n请稍后,系统自动返回!!\n");
} 展开
void loadfromfile(struct stu *a,struct xibu2 *r,int n,int k)
{
int h;
FILE* fp;
FILE* fp1;
system("cls");
if((fp=fopen("studentinfo.txt","r"))!=NULL)
fseek(fp,49L,0);
while(!feof(fp))
{
fscanf(fp,"%s\t",a[m].num);
fscanf(fp,"%s\t",a[m].name);
fscanf(fp,"%c\t",&a[m].sex);
fscanf(fp,"%d\t",&a[m].age);
fscanf(fp,"%s\t",a[m].xibu);
fscanf(fp,"%s\t",a[m].zhuanye);
fscanf(fp,"%s\t",a[m].classs);
fscanf(fp,"%f\t",&a[m].score[0]);
fscanf(fp,"%f\t",&a[m].score[1]);
fscanf(fp,"%f\t",&a[m].score[2]);
m++;
}
fclose(fp);
if((fp1=fopen("xibu.txt","r"))!=NULL)
fseek(fp1,21L,0);
while(!feof(fp1))
{
fscanf(fp1,"%s\t",r[f].name);
fscanf(fp1,"%d\t",bj[f]);
for(h=0;h<bj[f];h++)
{
fscanf(fp1,"%s\t",r[f].banji[h]);
}
f++;
}
fclose(fp1);
printf("读取文件数据成功!\n请稍后,系统自动返回!!\n");
} 展开
展开全部
程序发出来看看?
=====================
由于你的程序我不能运行,根据程序分析会不会是这种情况导致,
你的文件是当时结构体形式存的,这样,读取的时候也都是有相应的格式,
fseek这个就是将文件指针移到相应的位置.是不是移了后,移了后,和你想取的值对不上了导致的啊.
比如,1\tname\t2
你移到了name的位置,但是你按整型取.会不会是这样造成?
另外,第二次读文件的时候
fscanf(fp1,"%d\t",bj[f]);
bj[f]前没取地址
=====================
由于你的程序我不能运行,根据程序分析会不会是这种情况导致,
你的文件是当时结构体形式存的,这样,读取的时候也都是有相应的格式,
fseek这个就是将文件指针移到相应的位置.是不是移了后,移了后,和你想取的值对不上了导致的啊.
比如,1\tname\t2
你移到了name的位置,但是你按整型取.会不会是这样造成?
另外,第二次读文件的时候
fscanf(fp1,"%d\t",bj[f]);
bj[f]前没取地址
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询