简体   繁体   English

如何使用 C 解析 CSV 文件中的每一列

[英]How to parse each column in a CSV file using C

I'm trying to use C to read a CSV file, iterate line by line (until EOF), and delimit/split each line by the comma.我正在尝试使用 C 来读取 CSV 文件,逐行迭代(直到 EOF),并用逗号分隔/拆分每一行。 Then I wish to separate each column into "bins" and put add them to a struct (which isn't shown here; I defined it in a helper file) based on type.然后我希望将每一列分成“bins”,并将它们添加到基于类型的结构(此处未显示;我在帮助文件中定义了它)。

For example, if I have 1,Bob , I'd like to split 1 and Bob into two variables.例如,如果我有1,Bob ,我想将1Bob分成两个变量。 Here's what I've written so far.这是我到目前为止所写的内容。

void readFile(char file[25]) {
    FILE *fp;
    char line[1000];

    fp = fopen(file, "r"))

    while(fgets(line, 1000, fp)) {
        char* tmp = strdup(line);
        char* token;

        while((token = strsep(&tmp, ","))) {
            printf("%s\n", token);  // I want to split token[0] and token[1]
        }
    }
    fclose(fp);
}

T he above code does compile and run.上面的代码确实可以编译和运行。 I just don't know how to access each split of the token , like token[0] or token[1] .我只是不知道如何访问token每个拆分,例如token[0]token[1] In python, this would be simple enough.在python中,这很简单。 I could just access 1 using token[0] and Bob using token[1] for each line .对于每一line我只能使用token[0]访问1Bob使用token[1]访问1 But here in C, I can't do that.但是在 C 中,我不能这样做。

For testing purposes, all I'm doing right now is printing each line (in the second while loop), just to see how each split looks.出于测试目的,我现在所做的只是打印每一行(在第二个while循环中),只是为了查看每个拆分的外观。 I haven't implemented the code where I put each split line into its respective struct member.我还没有实现将每个分割线放入其各自结构成员的代码。

I've searched Stack Overflow and found a multitude of threads on this topic.我搜索了 Stack Overflow 并找到了许多关于这个主题的主题。 None of them seemed to help me except for this one , which I have drawn from.除了我从中汲取的这个之外,他们似乎都没有帮助我。 But I wasn't able to get the storing of split columns working.但是我无法让拆分列的存储工作。

In python, this would be simple enough.在python中,这很简单。 I could just access 1 using token[0] and Bob using token[1] for each line .对于每一line我只能使用token[0]访问1Bob使用token[1]访问1 But here in C, I can't do that.但是在 C 中,我不能这样做。

Yes, you can, if only you define the array.是的,你可以,只要你定义数组。

    while (fgets(line, sizeof line, fp))
    {
        char *tmp = strchr(line, '\n');
        if (tmp) *tmp = '\0';   // remove the '\n'
        tmp = strdup(line);
        #define MAXCOLUMNS  2
        char *token[MAXCOLUMNS];
        int c = 0;
        while (tmp)
        {
            if (c == MAXCOLUMNS) puts("too many columns"), exit(1);
            token[c++] = strsep(&tmp, ",");
        }
        if (1 <= c) printf("column 1: %s\n", token[0]);
        if (2 <= c) printf("column 2: %s\n", token[1]);
        // ONLY if the line's tokens are no longer needed:
        free(*token);
    }

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

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