文件的读取出现问题,求佬们解救一下
嵌套结构体:
typedef struct major {
char course[15];
int score;
int paiming;//排名
struct major* pre;
struct major* next;
}major;
typedef struct student {
char name[20];
char student_ID[20];
char sex;
char classs[10];
major* pPre;
struct student* pre;
struct student* next;
}stu;
read函数代码(n是全局变量,用来记录学生数量):
void read() {
stu* s, * r;
major* s1, * r1;
r = pH;
int i=0;
FILE* fp;
char filename[20 + 1];
printf("请输入要打开的文件名:\n");
scanf("%s", filename);
rewind(stdin);
if ((fp = fopen(filename, "r")) == NULL)
{
printf("打开文件失败!\n");
}
else {
while (!feof(fp)) {
s = (stu*)malloc(sizeof(stu));
s->pPre = (major*)malloc(sizeof(major));
fscanf(fp, "%s %s %c %s ", &s->name, &s->student_ID, &s->sex, &s->classs);
printf("%s %s %c %s ", s->name, s->student_ID, s->sex, s->classs);
r1 = s->pPre;
while (getc(fp) != '\n') {
s1 = (major*)malloc(sizeof(major));
fscanf(fp, "%s %d %d ", &s1->course, &s1->score, &s1->paiming);
printf("%s %d %d ", s1->course, s1->score, s1->paiming);
r1->next = s1;
s1->pre = r1;
r1 = s1;
}r1->next = NULL;
printf("\n");
r->next = s;
s->pre = r;
r = s;
i++;
}r->next = NULL;
n = i;
printf("读取成功!!!\n");
fclose(fp);
system("pause");
}
}
我想的是通过读取换行符来跳出课程链表,继续学生链表的读取。但是运行成下面这样了。