C语言中的fread函数读取结构体,求助大神!

现有一个结构体,里面有多种数据类型,然后我想用fread函数读取txt赋值给这个结构体。
Student1
500
100 80 100 80 100 80 100 80
100 80 100 80 100 80 100 80
100 80 100 80 100 80 100 80
100 80 100 80 100 80 100 80
100 80 100 80 100 80 100 80
这是我的txt文件内容,代码如下,调试结果为所有东西全部在char name[10]中,int类型的变量没有值,好久没用过C了,不知道问题出在哪里?

#include "stdafx.h"
#include <stdlib.h>
struct scoreinfo
{
int wscore;
char name[7];
int inter[5][8];
}*p;
int _tmain(int argc, _TCHAR* argv[])
{
struct scoreinfo *p;
p=(struct scoreinfo*)malloc(sizeof(struct scoreinfo));
FILE *fp;
fp=fopen("Student1.txt","rb");
int j=sizeof(scoreinfo);
fread(p,sizeof(scoreinfo),1,fp);
fclose(fp);

return 0;
}

fread是读取2进制文件的,不要直接使用它读取文本文件,因为需要转换。
最好使用,fscanf。
如果需要提高效率,可以使用fread+sscanf或者fread+atoi
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-07-24
18100 48200 112
第2个回答  2012-07-24
I dont think you can read everything at the same time
Try read each element once a time.
第3个回答  2012-07-24
int wscore;
char name[7];
把这两个换个位置试试
相似回答