Reputation: 37
I was learning about linked list and how to use it, and i make a simple program that store and prints the age and the name of a studen with this approach. i wanted to do other thing, and i thought to store this data from the students inside a text file. But when i try to write to this file, it throws an semengation fault error.
here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
typedef struct student {
char * name;
int age;
struct student *next;
}student;
student * createStudent(char *, int);
student * append(student *, student *);
void printStudents(student *, int);
void storeStudents(student *, int, int flag);
int main(){
student *end, *start , *newStudent, *tmp;
int studentAge, nStudents, i = 1;
char * studentName = (char *)malloc(sizeof(char) *50);
if(!studentName){
printf("Couldn't allocate the memory\n");
exit(EXIT_FAILURE);
}
printf("Enter the number of students : ");
scanf("%d", &nStudents);
while(nStudents > 0){
printf("Enter the age from the %d student: ", i);
scanf("%d", &studentAge);
printf("Enter the name from the %d student: ", i);
scanf("%s", studentName);
if((i-1) ==0){
start = createStudent(studentName, studentAge);
end = start;
storeStudents(start, i , 0);
printStudents(start, i);
}else{
newStudent = createStudent(studentName, studentAge);
end = append(end, newStudent);
storeStudents(end, i , 1);
printStudents(end, i);
}
nStudents--;
i++;
}
tmp = start -> next;
free(start);
start = tmp;
tmp = start->next;
free(start);
free(tmp);
return 0;
}
void printStudents(student * start, int i){
if(start != NULL) printf("Student number %d\nName: %s\nAge: %d\n", i, start -> name, start -> age);
return;
}
void storeStudents(student * ptr, int i, int flag){
FILE *pfile = NULL;
switch (flag){
case 0: pfile = fopen("students.txt", "w+"); break;
case 1: pfile = fopen("students.txt", "a+"); break;
}
if(!pfile){
fprintf(stderr, "Couldn't open the file\n");
exit(EXIT_FAILURE);
}
else if(ptr != NULL){
fprintf(pfile, "Student number %d\nName: %s\nAge: %s\n", i, ptr -> name, ptr -> age);
}
fclose(pfile);
pfile = NULL;
return;
}
student * createStudent(char * name, int age){
student * ptr = NULL;
ptr = (student *) malloc(sizeof(student));
ptr->name = (char *)malloc(sizeof(char) *50);
if(!ptr || !(ptr ->name)){
printf("Couldn't allocate the memory\n");
exit(EXIT_FAILURE);
}
ptr-> age = age;
ptr ->name = name;
ptr ->next = NULL;
return ptr;
}
student * append(student * end, student * new){
end->next = new;
end = new;
return end;
}
the error occurs in the function storeStudents.
what is causing this error?
Upvotes: 1
Views: 52
Reputation: 12515
Ok, I ran this code with an online C compiler / debugger and it told me the segfault is on the fprintf() line where you have mismatched your types string / int type on age output.
This in combination with your createStudent not doing a string copy and not freeing all your malloc'd memory all are contributing bugs in your application.
I re-ran your code with:
fprintf(pfile, "Student number %d\nName: %s\nAge: %d\n", i, ptr -> name, ptr -> age);
and it no longer segfaults.
Please fix the other issues as well though.
Upvotes: 2