In my code I need to build a function that's going to parse the content of a file and transform it into structures. The issue is that in that function in particular, on the first loop of the while, the resultats is going to have the right value in the array titres, but as soon as the new line is read (fgets(line, MAX_LINE_LENGTH, file)), and the value of line changes, the value inside resultats->titres changes too. I'm relatively new to C and can't figure out why as I manage to have my functions working elsewhere(see end of post). I have a feeling it's related to my limited understanding of pointers but I can't seem to figure out how to fix it !
If anyone could help me it would be greatly appreciated !
######## Code ########
structures: I didn't include it all but both structures have some getters and setters as well as a constructor and destructor
struct titre {
char* tconst;
char* primaryTitle;
char* genres;
char* titleType;
int startYear;
};
typedef struct titre* t_titre;
struct resultats {
int size;
t_titre titres;
};
typedef struct resultats* t_resultats;
t_titre creer_titre(void) {
t_titre titre;
titre = (t_titre) malloc(sizeof(struct titre));
memset(titre, 0, sizeof(*titre));
if (titre) {
titre->tconst = NULL;
titre->primaryTitle = NULL;
titre->genres = NULL;
titre->titleType = NULL;
titre->startYear = -1;
}
return titre;
}
...
void set_tconst(t_titre titre, char* tconst) {
titre->tconst = tconst;
}
...
Not working implementation:
#include "imdb.h"
#define MAX_LINE_LENGTH 128
void parse_line(t_titre titre, char* line);
void filter_results(t_resultats results, t_critere filter);
t_resultats search_file(t_critere criteres, char* file_name) {
FILE* file = fopen(file_name, "r");
t_resultats resultats = creer_resultats();
t_titre titre = creer_titre();
if (file == NULL) {
printf("Couldn't open file %s\n", file_name);
exit(EXIT_FAILURE);
}
char line[MAX_LINE_LENGTH];
fgets(line, MAX_LINE_LENGTH, file); //Skip header line
while (fgets(line, MAX_LINE_LENGTH, file)) {
parse_line(titre, line);
add_titre(resultats, titre);
}
free(titre);
fclose(file);
return NULL;
}
void parse_line(t_titre titre, char* line) {
line[strcspn(line, "\n")] = 0; //Remove trailling \n
set_tconst(titre, strsep(&line, "\t"));
set_titleType(titre, strsep(&line, "\t"));
set_primaryTitle(titre, strsep(&line, "\t"));
strsep(&line, "\t"); //Skip originalTitle
strsep(&line, "\t"); //Skip isAdult
set_startYear(titre, strtol(strsep(&line, "\t"), NULL, 10));
strsep(&line, "\t"); //Skip endYear
strsep(&line, "\t"); //Skip runtimeMinutes
set_genres(titre, line); //Use remaining of line as it is the last value
}
void filter_results(t_resultats results, t_critere filter) {
}
Working implementation:
t_resultats resultats = creer_resultats();
t_titre t = creer_titre();
set_tconst(t, "tt0000001");
set_titleType(t, "short");
set_primaryTitle(t, "It");
set_startYear(t, 2015);
set_genres(t, "Horror,Short");
add_titre(resultats, t);
set_tconst(t, "tt0000002");
set_titleType(t, "short");
set_primaryTitle(t, "ET");
set_startYear(t, 2015);
set_genres(t, "Horror,Short");
add_titre(resultats, t);
set_tconst(t, "tt0000003");
set_titleType(t, "short");
set_primaryTitle(t, "Interstellar");
set_startYear(t, 2015);
set_genres(t, "Sci-Fi,Short");
add_titre(resultats, t);