之前用数组的数据填进链表都没有问题,为什么这次的字符串会不适用呢?
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include"student.h"
typedef struct course
{
char name[5];
int ID;
int section;
}course;
typedef struct student
{
char Firstname[100];
char Lastname[100];
char SSN[15];
int number;//number of course attending
course Arraycourse[10];
struct student* next;
}student;
void ReadInputFile(char* filename, student* stu)
{
FILE* fp;
fopen_s(&fp, filename, "rt");
if (!fp)
{
printf("open file error");
}
stu = (student*)malloc(sizeof(student) * 10);
for (int i = 0; i < 2; i++)
{
fscanf_s(fp, "%s%s%s%d", stu[i].Firstname, sizeof(stu[i].Firstname),
stu[i].Lastname, sizeof(stu[i].Lastname),
stu[i].SSN, sizeof(stu[i].SSN),
&stu[i].number);
printf("%s\n%s\n%s\n%d\n", stu[i].Firstname,
stu[i].Lastname,
stu[i].SSN,
stu[i].number);
for (int j = 0; j < stu[i].number; j++)
{
fscanf_s(fp, "%s%d%d", stu[i].Arraycourse[j].name, sizeof(stu[i].Arraycourse[j].name),
&stu[i].Arraycourse[j].ID, &stu[i].Arraycourse[j].section);
printf("%s %d %d\n", stu[i].Arraycourse[j].name,
stu[i].Arraycourse[j].ID, stu[i].Arraycourse[j].section);
}
}
fclose(fp);
}
void AddToList(student* addStudent, student* head)
{
student* prev, * p;
prev = (student*)malloc(sizeof(student));
head = prev;
for (int i = 0; i < 10; i++)
{
p = (student*)malloc(sizeof(student));
p->Firstname = addStudent[i].Firstname;
p->Lastname = addStudent[i].Lastname;
p->SSN = addStudent[i].SSN;
p->number = addStudent[i].number;
for (int j = 0; j < p->number; j++)
{
p->Arraycourse[j].name = addStudent[i].Arraycourse[j].name;
p->Arraycourse[j].ID = addStudent[i].Arraycourse[j].ID;
p->Arraycourse[j].section = addStudent[i].Arraycourse[j].section;
}
p->next = NULL;
prev->next = p;
prev = p;
}
}