다른 프로그램으로 만든 학생 데이터(1000개 이하 임의로 생성)를 읽어들이고, 정수 하나 입력받아서 해당 위치의 학생데이터를 출력하는 코드인데


예컨대 800개를 만들면 801번째는 데이터가 없을수도 있단말이야


그러면 segmentation fault 에러가 뜨는데 이 에러 대신 학생 데이터가 없다는 에러메시지를 내가 대신 출력해주려면 어떻게 조건을 줘야할까? 도와줘 형들..


#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

struct pscore{
int num;
char nme[10];
float sc1;
float sc2;
float sum;
};

struct node
{
struct pscore data;
struct node* next;
};

void insert(struct node* head,struct pscore* val)
{
while(head != NULL)
{
if(head->next == NULL)
{
struct node* new = malloc(sizeof(struct node));
new->data.num = val->num;
strcpy(new->data.nme, val->nme);
new->data.sc1 = val->sc1;
new->data.sc2 = val->sc2;
new->data.sum = val->sum;
new->next = NULL;
head->next = new; // 다음 노드로
break;
}
head = head->next;
}
}

struct node* get(struct node* head, int index)
{
for (int i = 0; i<index; i++)
{
head = head->next;
}
return head;
}

void unmalloc(struct node* head)
{
while (head != NULL)
{
struct node* temp = head->next;
free(head);
head = temp;
}
}
// 모든 노드의 동적메모리 순회하며 해제

int main(int argc, char* argv[]) {

FILE *fp;
int ret;
int input;

if (argc != 2) {
printf("< Usage: ./loader filename> ");
return 1;
}

if((fp = fopen(argv[1], "rb")) == NULL){
perror("Open");
exit(1);
}

struct node* head = malloc(sizeof(struct node));
head->next = NULL;
do
{
struct pscore data;
ret = fread(&data, sizeof(struct pscore), 1, fp);
insert(head, &data);
} while(ret>0); // 가능할때까지 읽음

printf("1000이하의 양의 정수 하나를 입력하십시오: ");
scanf("%d",&input);

struct node* numnode = get(head, input);
if() // <-고민하는부분
{
printf("there is no student data");
}
else{
printf("학번: %d ",numnode->data.num);
printf("이름: %s ",numnode->data.nme);
printf("점수1: %d ",(int)numnode->data.sc1);
printf("점수2: %d ",(int)numnode->data.sc2);
printf("점수합계: %d ",(int)numnode->data.sum);
}
unmalloc(head);
fclose(fp);

return 0;
}