简体   繁体   中英

How to read the first entries of a CSV file in C?

I have a csv file that looks like this:

Jake, 25, Montreal
Maria, 32, London
Alex, 19, New York
Jake, 22, Dubai

The function I'm trying to implement is find_name that should iterate through the first field of each record and compare it to the name that is being searched for.

I've tried fgets, fscanf, but either the code doesn't work or I get a segmentation fault.

This is what I have so far:

void find_name(const char *csv_filename, const char *name){
    FILE *csvFile = fopen(csv_filename, "r");
    char word[1000];

    if (csvFile == NULL)
            exit(EXIT_FAILURE);

    while ( !feof(csvFile) ) {
            fscanf(csvFile, "%s%*[^,]", word);
            if ( strcmp(word, name) == 0 )
                    printf("name found");
    }
    fclose(csvFile);
}

Any help is appreciated.

EDIT: I would not like to use any tokenizer function, I'd rather understand how to use fscanf.

If you read one field at a time, it becomes quite tricky to deal with the end of a line, so suggest you take it a line at a time, something like:

int FieldScanCount = 0;
char city[1000];
int age = 0;
while ((FieldScanCount = fscanf(csvFile, "%1000[A-Za-z0-9 ],%i,%1000[A-Za-z0-9 ]\r\n", &word, &age, &city)) > 0) {

I've assume there is a \\r\\n at the end of each line, but depending on your your file this might need just \\n.

regarding:

 while ( !feof(csvFile) ) {
        fscanf(csvFile, "%s%*[^,]", word);
        if ( strcmp(word, name) == 0 )
                printf("name found");
}

suggest using:

while ( fgets( word, sizeof(word), csvFile ) )
{
    char *token = strtok( word, ", " );
    if( strcmp( token, name )  == 0 )
    {
         printf("name found");
    }
}

However, if you do not want to use strtok() then suggest:

while ( fgets( word, sizeof word, csvFile ) )
{
    char *comma = strchr( word, ',');
    *comma = \0';

    if( strcmp( word, name )  == 0 )
    {
         printf("name found");
    }
}

however, if you really want to use the scanf() family of functions:

while ( fgets( word, sizeof word, csvFile ) )
{
    char possibleMatch[1000];
    if( sscanf( "%999[^,]", word, possibleMatch ) == 1 )
    {
        if( strcmp( possibleMatch, name )  == 0 )
        {
            printf("name found");
        }
    }
}

However, if you really want to use fscanf() :

while ( fscanf( csvFile, "%999[^,]", word ) == 1 )
{
    if( strcmp( word, name )  == 0 )
    {
        printf("name found");
    }

    //consume rest of input file line
    int ch;
    while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
}

or perhaps even better:

while ( fscanf( csvFile, " %999[^,] %*[^\n]", word ) == 1 )
{
    if( strcmp( word, name )  == 0 )
    {
        printf("name found");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM