Home > Enterprise >  Getting segmentation fault (core dumped) while trying to read a csv file in C
Getting segmentation fault (core dumped) while trying to read a csv file in C

Time:11-14

I have a csv file with 10000 lines and when I debugged the code I noticed I was getting a segmentation fault on the last line. If I delete it the error disappears (Sorry for my english).

struct drivers {
    char *id;
    char *name;
    char *birth_day; 
    char *gender; 
    char *car_class;
    char *license_plate;
    char *city;
    char *account_creation;
    char *account_status; 
};

typedef struct drivers *Drivers;

Drivers initDriver (char *id, char *name, char *birth_day, char *gender, char *car_class, char *license_plate, char *city, char *account_creation, char *account_status) {
    Drivers driver = malloc(sizeof(struct drivers));
    driver -> id = strdup(id);
    driver -> name = strdup(name);
    driver -> birth_day = strdup(birth_day);
    driver -> gender = strdup(gender);
    driver -> car_class = strdup(car_class);
    driver -> license_plate = strdup(license_plate);
    driver -> city = strdup(city);
    driver -> account_creation = strdup(account_creation);
    driver -> account_status = strdup(account_status);
    printf("%s;%s;%s;%s;%s;%s;%s;%s;%s", id, name, birth_day, gender, car_class, license_plate, city, account_creation, account_status);
    return driver;
}

Drivers initDriverFromLine (char *linha) {
    char *id = (strsep(&linha,";"));
    char *name = (strsep(&linha,";"));
    char *birth_day = (strsep(&linha,";"));
    char *gender = (strsep(&linha,";"));
    char *car_class = (strsep(&linha,";"));
    char *license_plate = (strsep(&linha,";"));
    char *city = (strsep(&linha,";"));
    char *account_creation = (strsep(&linha,";"));
    char *account_status = (strsep(&linha,";"));
    
    return initDriver(id, name, birth_day, gender, car_class, license_plate, city, account_creation, account_status);
}

int main () {
    FILE* Modulo1 = fopen("drivers.csv","r");
    if (Modulo1 == NULL) printf("Erro ao abrir o ficheiro.\n");

    char linha[1024];
    while (fgets(linha, sizeof(linha), Modulo1) != NULL) {
        initDriverFromLine(fgets(linha, sizeof(linha), Modulo1));
    }
    fclose(Modulo1);

    return 0;
}

My objective is to store the drivers into a linked list but as I'm trying to read the last line I get a segmentation fault error.

CodePudding user response:

In the fgets loop you don't use the string that was read in the while() condition, but fgets another one and process it without checking if that is NULL. So you'll process every second line in the file, discard the others, and the last iteration (if the file has an odd number of lines) will segfault, because fgets returns NULL.

The loop should be

while (fgets(linha, sizeof(linha), Modulo1) != NULL) {
    initDriverFromLine(linha);
}

You don't make use of the return value from initDriverFromLine.

Also, in initDriver () you have assigned every strdup to driver -> id. You then print all the other members, but they are undefined pointers. Is that a copy/paste error?

  • Related