求指教啊 出线段错误

mygill 2011-03-28 01:06:39
把一个文本的内容读到 一个结构体数组里
TXT 内容:
1 li chinese 40
1 li max 30
2 liu chinese 77
3 zhang max 88
3 zhang chinese 89
2 liu max 100

#include<stdio.h>

#define N 50


int maxnum = 0;

typedef struct
{
int Id;
char Name[10];
float TotalScore;
}student;

student Stu[N];

void Load(const char* PATH,student *Stu)
{
FILE *fd;
int i,Flag;
struct temp
{
int Id;
char Name[10];
char Oject[10];
float Score;
}*stu;
fd = fopen(PATH,"r");

while(feof(fd))
{
Flag = 0;
fscanf(fd,"%d",stu->Id);
fscanf(fd,"%s",stu->Name);
fscanf(fd,"%s",stu->Oject);
fscanf(fd,"%s",stu->Score);

for(i = 0;i<maxnum;i++)
if(Stu[i].Id == stu->Id)
{
Stu[i].TotalScore += stu->Score;
Flag++;
}
if(Flag = 0)
{
Stu[maxnum].Id = stu->Id;
strcpy(Stu[maxnum].Name,stu->Name);
Stu[maxnum].TotalScore = stu->Score;
maxnum++;
}
}
fclose(fd);
}


int main()
{
Load("text.txt",Stu);
printf("%d,%s,%d\n",Stu[0].Id,Stu[0].Name,Stu[0].TotalScore);
return 0;
}



运行段错误!!
...全文
123 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
luciferisnotsatan 2011-03-28
  • 打赏
  • 举报
回复
单步调试
赵4老师 2011-03-28
  • 打赏
  • 举报
回复
struct temp
{
int Id;
char Name[10];
char Oject[10];
float Score;
} s,*stu;
stu=&s;
赵4老师 2011-03-28
  • 打赏
  • 举报
回复
fscanf(fd,"%d",&stu->Id);
fscanf(fd,"%s",stu->Name);
fscanf(fd,"%s",stu->Oject);
fscanf(fd,"%f",&stu->Score);
...
if (Flag==0)
AnYidan 2011-03-28
  • 打赏
  • 举报
回复
LZ 但步跟踪一下,看看何处与你的想法不一致,
Johnxme126 2011-03-28
  • 打赏
  • 举报
回复
stu没开辟空间
fscanf(fd,"%s",stu->Score);//Score是float
if(Flag = 0)//不用说了
jackiesen 2011-03-28
  • 打赏
  • 举报
回复
for(i = 0;i<maxnum;i++)?maxnum是全局变量?
pengzhixi 2011-03-28
  • 打赏
  • 举报
回复
stu=(temp*)malloc(sizeof(temp));
newfarmerchi 2011-03-28
  • 打赏
  • 举报
回复
加个它#include <string.h>
newfarmerchi 2011-03-28
  • 打赏
  • 举报
回复
if(Flag = 0)它肯定是一个问题。
jiangsq12345 2011-03-28
  • 打赏
  • 举报
回复
。。。。没有其他信息吗?
这样排错太难了,
楼主可以把代码内容裁剪,从最简单开始,能运行了就再加一段,这样找错容易点,也可以多用printf输出

以后有时间最好学下gdb,最使用于这种场合
mygill 2011-03-28
  • 打赏
  • 举报
回复
上面贴的代码里 feof(fd) 应为 !feof(fd) 贴错了


高手快来啊

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧