
C语言中关于fseek的问题
我在一个函数总两次调用了fseek函数,都是fseek(fp,49L,0);但如果把第一个fseek的49L换成其他就可以运行,如果把第二个fseek的49L换成21L就...
我在一个函数总两次调用了fseek函数,都是fseek(fp,49L,0);但如果把第一个fseek的49L换成其他就可以运行,如果把第二个fseek的49L换成21L就不能正常读取文件了,错误如图:很想不通这是为什么?求各位高手帮忙
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]);
fscanf(fp,"%f\t",&a[m].averge);
fscanf(fp,"%f\t",&a[m].sum);
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("读取 展开
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]);
fscanf(fp,"%f\t",&a[m].averge);
fscanf(fp,"%f\t",&a[m].sum);
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("读取 展开
1个回答
展开全部
请把程序发上来观摩
-----
我看了,代码从逻辑和语法上没有出错,那你就要检查下自己定义的结构数组了,一般情况下数组越界会出现运行时错误。
因为你之前提到49L没有错,而换到21L就有错了,很有可能是读出来的数据存放在你的结构数祖里时发生了错误,你仔细再看看
-----
我看了,代码从逻辑和语法上没有出错,那你就要检查下自己定义的结构数组了,一般情况下数组越界会出现运行时错误。
因为你之前提到49L没有错,而换到21L就有错了,很有可能是读出来的数据存放在你的结构数祖里时发生了错误,你仔细再看看
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询