简体   繁体   English

如何在C中读取CSV文件的第一个条目?

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

I have a csv file that looks like this: 我有一个看起来像这样的csv文件:

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. 我正在尝试实现的函数是find_name,它应遍历每条记录的第一个字段,并将其与正在搜索的名称进行比较。

I've tried fgets, fscanf, but either the code doesn't work or I get a segmentation fault. 我尝试了fgets,fscanf,但是代码不起作用或者我遇到了分段错误。

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. 编辑:我不想使用任何tokenizer功能,我宁愿了解如何使用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. 我假设每行末尾有一个\\ r \\ n,但根据你的文件,这可能只需要\\ 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: 但是,如果你不想使用strtok()那么建议:

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: 但是,如果你真的想使用scanf()系列函数:

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() : 但是,如果你真的想使用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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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