在C中读取.CSV文件

时间:2014-09-24 07:54:20

标签: c csv

我要回到我大学的C一年的Java,我们的第一个任务是读取.CSV文件中的值,但教科书不清楚,教授没有&# 39; t帮助了我们很多。我真的没有很多方向,很快就会完成任务,所以我真的需要一些方向!

我想我自己可以完成大部分工作,但我不确定这段代码是做什么的......

static int extractItems(char *line, char row[][MAXLEN]) {
    char *item;
    int col = 0;
    for( ; ; ) {
        item = strtok(line, ",\r\n");
        if (item == NULL)
            break;
        if (col >= MAXCOLS) {
            tooWide = 1;
            break;
        }
        strncpy(row[col], item, MAXLEN);
        row[col][MAXLEN] = '\0'; // force null termination
        col++;
        line = NULL;  // required by strtok function
    }
    return col;
}

Col指的是列号,第一个是0。

据我所知,它检查线路中是否有任何东西以及它是否太宽,但其余部分对我来说都是陌生的。

2 个答案:

答案 0 :(得分:1)

我认为最好的解释方法是对代码进行评论:

static int extractItems(char *line, char row[][MAXLEN]) {
                                      //line is a char* to your array of chars
                                                     // - one long string
                                      //row is a two dimensional array of chars
                                                     // - an array of strings
    char *item;
    int col = 0;
    for( ; ; ) {                      //infinite loop, we can exit it with "break" though
        item = strtok(line, ",\r\n"); //string->token.  Returns a pointer to a string,
                                      //which is the next token in the input.
                                      //it does this by replacing the first character found 
                                      //from the second string passed (",\r\n") with '\0'
                                      //and returning the pointer to where it started 
                                      //searching from.
                                      //i.e. it cuts the string up into substings (tokens)
                                      //and returns a pointer to the next one each time 
                                      //it is called.
        if (item == NULL)             //if NULL we are at end of the line. so exit loop
            break;
        if (col >= MAXCOLS) {         //if we have read too much (reached our limit) then exit
            tooWide = 1;              //is this a global? anyway it is used to signal that there was too much data
            break;
        }
        strncpy(row[col], item, MAXLEN); //copy the temporary string returned by strtok to the array
        row[col][MAXLEN] = '\0';      // force null termination (C_string remember?)
        col++;                        // increment the number of words counted
        line = NULL;                  // required by strtok function
                                          //passing in NULL gets strtok to continue scanning
                                          //from the end of the previous successful scan
    }
    return col;
}

有关strtok的更多信息:this answer describes it well

答案 1 :(得分:0)

让我们打破程序并进行分析,

static int extractItems(char *line, char row[][MAXLEN]) {

line是一个数组,其中包含从CSV文件中读取的当前行 row是一个二维数组,其中每个项目都是CSV文件中的单个元素

 item = strtok(line, ",\r\n");

strtok函数根据分隔符字符串,第二个参数,此处line将字符串,第一个参数,此处,\n\r拆分为标记 也就是说,如果CSV文件包含行
Hello, world 然后用线条生成调用strtok Hello =>分裂线, world =>分割线在\ n,\ r \ n在windows中用作换行符
For further reading refer this link

现在,item中的每个元素都会被row复制到strncpy,复制n,MAXLEN字符。

strncpy(row[col], item, MAXLEN); 
row[col][MAXLEN] = '\0';

然后由\0

分发
col++;                       
line = NULL;

增加到下一个项目的下一个位置,并将line设置为NILL以供下一个项目使用。

由于数组row作为默认值通过引用传递,因此一旦程序运行,row将包含CSV文件中的每个逗号分隔值。