【发布时间】:2019-09-23 17:59:01
【问题描述】:
我正在构建一个简单的硬编码 csv 解析器,如下所示:
typedef struct {
char * id;
char * date;
} ROW;
int main(int argc, char *argv[]) {
// Load a file that is of a known csv format
// With fields separated by ","
// And allow a max line length of 255 chars
FILE * fp = fopen("test100k.csv", "r");
char* FIELD_SEPARATOR = ",";
char buffer[255];
int row_num = 0;
ROW row;
ROW * rows = malloc(sizeof(row) * 120000);
// The first line is the header so we can just skip it
(void) fgets(buffer, sizeof(buffer), fp);
while(fgets(buffer, sizeof(buffer), fp) != NULL) {
// split the row
char *token;
token = row.id = strtok(buffer, FIELD_SEPARATOR);
int num_sep_encountered = 1;
while( token != NULL ) {
token = strtok(NULL, FIELD_SEPARATOR);
switch(num_sep_encountered) {
case 1: row.date = token;
}
num_sep_encountered ++;
}
printf("ID: %s | Date: %s\n", row.id, row.date);
rows[row_num++] = row;
}
}
我想更改它,以便我有第二个名为 ROWS 的结构,如下所示:
typedef struct {
ROW * row;
size_t size;
} ROWS;
我将如何在以下三个地方创建和初始化这个结构:
// initialize it -- how to do this?
ROWS rows= malloc(sizeof(row) * 120000);
// how keep appending a ROW to the ROWS in the for loop?
rows->row = row
// how to set the size
rows.size = row_num
【问题讨论】:
-
请记住,这允许最大行长度为 253 或 254,具体取决于您的平台。 CR+LF+NUL 或 LF+NUL。
-
所有行都有指向同一个
buffer数组的指针,您读取的每一行都会覆盖该数组。您需要使用strdup()复制它们。 -
把你的ROW结构做成链表不是更好吗?
-
@Barmar 应该改为
rows[row_num++] = strdup(row);吗?或者我应该在哪里改变它? -
@Shared
row.id = strdup(strtok(buffer, FIELD_SEPARATOR));和row.date = strdup(token);