I have to read data from a .txt file, and then sort them.
I created my method on an other C file, and it worked perfectly, but when I import it, I got this Segmentation fault error.
I try each line of my program, and it seems that my problem is coming from these two functions :
ListScore* fillScore(){
ListScore *list=malloc(sizeof(*list));
FILE* f =NULL;
int score;
char name[20];
f= fopen("score.txt", "r");
if (f==NULL || list==NULL) {
perror("fopen or list Null");
exit(EXIT_FAILURE);
}
while(fscanf(f,"%d%s",&score,name)==2){
insertElem(list,score,name);
}
fclose(f);
bubbleSort(list);
return list;
}
and
void bubbleSort(ListScore* list){
Score_s *get=list->first;
Score_s *move;
/* Checking for empty list */
if (list->first == NULL)
{return;}
else{
while(get->next!=NULL){
move=list->first;
while(move->next!=NULL){
if(move->score<move->next->score){
swap(move,move->next);
}
move=move->next;
printTest();
}
get=get->next;
}
}
}
and debugger show me this condition as the problem
if(move->score<move->next->score);
I can't understand why on an external C file, it worked, but in a complex project (with include.h) it doesn't work.
Thanks in advance.
EDIT : My struct ListScore is defined like this :
typedef struct ListScore{
Score_s *first;
}ListScore;
and Score_s like this
typedef struct Score_s Score_s;
struct Score_s{
char name[20];
int score;
Score_s *next;
};
EDIT 2 : Here's the minimal example, but it works perfectly on it's own... as said in the beginning of this question;
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
typedef struct Score_s Score_s;
struct Score_s{
char name[20];
int score;
Score_s *next;
};
typedef struct ListScore{
Score_s *first;
}ListScore;
void insertElem(ListScore* list,int s,char n[]){
if(list==NULL){
exit(EXIT_FAILURE);
}
Score_s* new;
new=malloc(sizeof(*new));
strcpy(new->name,n);
new->score=s;
new->next=list->first;
list->first=new;
}
void swap(Score_s *a,Score_s *b){
int tmp=a->score;
a->score=b->score;
b->score=tmp;
char tmpS[20];
strcpy(tmpS,a->name);
strcpy(a->name,b->name);
strcpy(b->name,tmpS);
}
void bubbleSort(ListScore* list){
Score_s *get=list->first;
Score_s *move;
/* Checking for empty list */
if (list->first == NULL)
{return;}
else{
while(get->next!=NULL){
move=list->first;
while(move->next!=NULL){
if(move->score<move->next->score){
swap(move,move->next);
}
move=move->next;
}
get=get->next;
}
}
}
void fillScore(){
ListScore *list=malloc(sizeof(*list));
FILE* f =NULL;
int score;
char name[20];
f= fopen("score.txt", "r");
if (f==NULL || list==NULL) {
perror("fopen or list Null");
exit(EXIT_FAILURE);
}
while(fscanf(f,"%s%19d",name,&score)==2){
insertElem(list,score,name);
}
Score_s* new;
new=malloc(sizeof(*new));
new=list->first;
fclose(f);
printf("\n\n");
bubbleSort(list);
new=list->first;
while(new!=NULL){
printf("%s %d\n",new->name,new->score);
new=new->next;
}
free(list);
}
int main(int argc, char const *argv[])
{
fillScore();
return 0;
}
Content of score.txt :
pat 20
ananna 20
radis 19
gg 121
nique 236
perie 125
aziz 127
telma 36
coc 1
aie 6
prout 236
and this is the output :
prout 236
nique 236
aziz 127
perie 125
gg 121
telma 36
ananna 20
pat 20
radis 19
aie 6
coc 1