#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
int id;
char name[20];
int score[4];
struct student *next;
};
int main()
{
int count = 0, id,sc[4];
char name[20],line[200];
struct student *p, *head, *node = NULL;
printf("학번과 이름을 입력하세요\n");
fgets(line, 200, stdin);
while (fgets(line, 200, stdin) != NULL) {
sscanf(line, "%d %s %d %d %d %d", &id, name,
&sc[0],&sc[1],&sc[2],&sc[3]);
p = (struct student *) malloc(sizeof(struct student));
node = (struct student *) malloc(sizeof(struct student));
if (head == NULL)
head = p;
else
node->next = p;
node = p;
if (p == NULL) {
perror("malloc");
exit(1);
}
p->id = id;
strcpy(p->name, name);
node=p->next;
for(int i=0; i< 4; i++)
p->score[i]=sc[i];
}
printf("\n* 학생 정보 *\n");
p = head;
while (p != NULL) {
count++;
printf("학번: %d 이름: %s : ", p->id, p->name);
for(int i=0; i< 4; i++)
printf("%6d", p->score[i]);
printf("\n");
p = p->next;
}
printf("총 %d 명입니다.\n", count);
exit(0);
}
//부분을 고쳐서 순차적으로? 정렬하는 건 어떻게 하나요??ㅠㅠㅠㅠ 파일 자체가 순차적으로 정렬이 되어있어서 노드로 이어주면서 출력을 하면 될 것 같은데 계속 안 돼서 질문 드립니다... 모바엑스텀에서 하는 걸로 txt 파일을 리다이렉션으로 실행 할 때 넣어주는 방법으로 실행시키고 있습니다. 이걸 실행시키면 세그멘테이션 오류나 39줄이 정렬 되어야하는데 한 줄만 나오는 그런 결과가 나와서 질문합니다ㅜㅜ