1

I am making a linked list of World Cup teams, when loading the teams I need to do a preload reading data from a csv file but reading 2-word countries makes it wrong

For example

Suppose that this is the csv file:

Arabia Saudita, Herve, Renard, Salman, C, 0, 1

First I read the country name, dt name, captain name, group and two numeric values that are part of the program, but the output is something like this:

Country:Arabia DT:Saudita Herve Renard Salman C 0 1 Captain: empty Group:Empty 

The expected output would be

Country: Arabia Saudita DtName:Herve DtSurname:Renard CaptainName:Salman Group: C

I tried to do it with a txt file but it is the same since it reads the spaces and the program fails or prints wrong

This is a part of the code that fails

    FILE *chargue = fopen("Precharge.csv", "r");
    
    while (!feof(charge)) {
        fscanf(charge, "%s\n", countryAux);
        chargecountry(&team, countryAux);
    
        fscanf(charge, "%s\n", nameDTAux);
        fscanf(charge, "%s\n", surnameDTAux);
        chargenameDT(&team, surnameDTAux, nameDTAux);
    
        chargeCapitan(&team, nameCapaux);
    
        fscanf(charge, "%c\n", &groupAux);
        chargegropu(&team, groupAux);
    
        fscanf(charge, "%d\n", &actualscoreaux);
        chargeactualscore(&team, actualscoreaux);
    
        fscanf(charge, "%d\n", &faseActualaux);
        chargeFase(&team, faseActualaux);
    
        insert(lis, team);
        forwards(lis);
    }
Kalifeh
  • 33
  • 4

1 Answers1

3

Your format strings parse single words and do not stop at the , separator.

Parsing the csv file (or any file in general) with fscanf() is not recommended as it is very difficult to recover from errors and newlines are mostly indistinguishable from other white space characters. Furthermore, it is incorrect to use while (!feof(charge)) to run this loop.

You can learn Why is “while( !feof(file) )” always wrong? and you should tell you teacher about this. It is a common mistake to use feof() for this pupose.

You should instead read one line at a time with fgets() and use %nnn[^,] conversion specifiers to parse the character fields (nnn being the maximum field length).

Here is an example:

#include <errno.h>
#include <stdio.h>
#include <string.h>

int main() {
    char buf[256];
    char countryAux[100];
    char nameDTAux[100];
    char surnameDTAux[100];
    char groupAux;
    int actualscoreaux, faseActualaux;
    ... // declare team, lis...

    FILE *chargue = fopen("Precharge.csv", "r");
    if (chargue == NULL) {
        fprintf(stderr, "cannot open %s: %s\n", "Precharge.csv",
                strerror(errno));
        return 1;
    }
    
    while (fgets(buf, sizeof buf, chargue)) {
        if (sscanf(buf, "%99[^,],%99[^,],%99[^,], %c ,%d ,%d",
                   countryAux, nameDTAux, surnameDTAux,
                   &groupAux, &actualscoreaux,&faseActualaux) == 6) {
            chargecountry(&team, countryAux);
            chargenameDT(&team, surnameDTAux, nameDTAux);
            chargeCapitan(&team, nameCapaux);
            chargegropu(&team, groupAux);
            chargeactualscore(&team, actualscoreaux);
            chargeFase(&team, faseActualaux);
            insert(lis, team);
            forwards(lis);
        } else {
            printf("invalid record: %s", buf);
        }
    }
    fclose(chargue);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • I didn't know that while(!feof) was wrong, that's the way they taught us in college,thanks – Kalifeh Nov 11 '22 at 21:32
  • @chux-ReinstateMonica: of course! unless the destination array has a length at least as great as the source string. – chqrlie Nov 11 '22 at 21:35
  • Nice. Minor: `"\n"` at the end of `sscanf(buf, "%99[^,],%99[^,],%99[^,], %c ,%d,%d\n"` does little. Perhaps `char junk; sscanf(buf, "%99[^,],%99[^,],%99[^,], %c ,%d, %d %c", .... &junk) == 6)` ? – chux - Reinstate Monica Nov 11 '22 at 21:37
  • @Kalifeh: I amended the answer with a link to an explanation regarding this mistake. They should not teach this in your college, try and point them to the reasons – chqrlie Nov 11 '22 at 21:38
  • 1
    @chux-ReinstateMonica: `chargegropu` is probably a typo, but consistent with the question. – chqrlie Nov 11 '22 at 21:40
  • @chux-ReinstateMonica: the trailing `\n` is a mistake and your suggestion is welcome if records with extra fields are to be rejected. The space before `%d` is redundant but might be useful before the `,`. – chqrlie Nov 11 '22 at 21:41