Программа должна читать список из файла, но выводит полный бред, а потом вобще вылетает
Код:
#include<stdio.h>
#include<stdlib.h>
#define N 3
struct student {
char name[50];
char surname[50];
int year;
struct student* next;
struct student* prev;
} ;
struct student*root=NULL;
struct student* AddToList(struct student* root,struct student* p){
if(NULL==p){
return root;
};
p->next=root;
p->prev=p->next;
return p;
};
struct student* INPUT_STUDENTS(student*root){
student*p=(student*)malloc(sizeof(student));
if (p==NULL) {
printf("error");
return 0;
};
FILE*f=fopen("student.txt","a+");
printf("vvedit name\n");
scanf("%s",p->name);
fprintf(f,"%s\n",p->name);
printf("vvedit surname\n");
scanf("%s",p->surname);
fprintf(f,"%s\n",p->surname);
printf("year\n");
scanf("%d",&(p->year));
fprintf(f,"%d\n",p->year);
fclose(f);
return p;
};
struct student*read(){
FILE*f=fopen("student.txt","a+");
while(!feof(f)){
struct student*p=(struct student*)malloc(sizeof(struct student));
fscanf(f,"%s",p->name);
fscanf(f,"%s",p->surname);
fscanf(f,"%d",&(p->year));
root=p;
};
fclose(f);
return root;
};
void PRINT_LIST(struct student*root){
while(root!=NULL) {
printf(" %s %s %d \n",root->name,root->surname,root->year);
root=root->next;
};
};
void main(){
int a;
printf("if you want add student to file press 1\nif you want read file press2\n");
scanf("%d",&a);
if(a==1){
for(int i=0;i<N;i++){
root=AddToList(root,INPUT_STUDENTS(root));
}
}
if(a==2)
root=read();
PRINT_LIST(root);
}