在 C 中的 strtok 之后无法正确计算 CSV 中的元素
Cannot properly count elements in CSV after strtok in C
在使用strtok 以逗号和引号(双引号)分隔后,我有时只能看到正确的元素数量计数。通常 printf 的长度为 0,但偶尔为 6 和 1,代码没有变化。
我试过只使用一个分隔符(逗号)并以不同方式定义 strtok 的标记输出,并重新排列 while 循环中语句的顺序,其中行的其他元素应该被分隔。这些是我用来测试代码的 .csv 文件的几行 (test.csv)。它与 NOAA 以 .csv 格式提供降水数据的格式相同。
"STATION","NAME","DATE","PRCP","PRCP_ATTRIBUTES"
"US183459384","XYZ ABC 9.0 E, WA US","2019-01-06","0.65",",,N"
"US183459384","XYZ ABC 9.0 E, WA US","2019-01-12","0.46",",,N"
"US183459384","XYZ ABC 9.0 E, WA US","2019-01-13","0.09",",,N"
"US183459384","XYZ ABC 9.0 E, WA US","2019-01-14","0.01",",,N"
"US183459384","XYZ ABC 9.0 E, WA US","2019-01-15","0.60",",,N"
"US183459384","XYZ ABC 9.0 E, WA US","2019-01-16","1.93",",,N"
下面是我的代码尝试。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
int get_row(FILE *file, int row_num, char delim[]) {
int n_line = 0;
int field = 0;
char row[BUFFER_SIZE], *line[BUFFER_SIZE];
while (fgets(row, BUFFER_SIZE, file)) {
if (n_line == row_num) {
printf("Length of line %d is %ld elements!\n", n_line, strlen(row));
char* element = strtok(row, delim);
while (element != NULL) {
printf("%s\n", element);
line[field++] = strdup(element);
element = strtok(NULL, delim);
}
return 0;
} else {
n_line++;
}
printf("There is no row %d in the file you selected.\n", row_num);
return 0;
}
int main(int argc, char **argv) {
FILE *file;
char delim[] = ", \"";
file = fopen(”test.csv”, "r");
if (!file) {
printf("Error: could not open %s\n", file_name);
return -1;
}
printf("Reading file...\n");
get_row(file, 0, delim);
fclose(file);
return 0;
}
我希望结果显示 5,但所有行的结果都是 0 或 1,偶尔显示 6。
程序中有许多语法错误。
这应该有效:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
int get_row(FILE *file, int row_num, char delim[]) {
char row[BUFFER_SIZE];
for (int i = 0; fgets(row, BUFFER_SIZE, file); i++) {
if (i == row_num) {
printf("Length of line %d is %ld elements!\n", i, strlen(row));
char* element = strtok(row, delim);
while (element) {
printf("%s\n", element);
element = strtok(NULL, delim);
}
break;
}
}
printf("There is no row %d in the file you selected.\n", row_num);
return 0;
}
int main() {
FILE *file;
char delim[] = ", \"";
file = fopen("test.csv", "r");
if (!file) {
puts("Error: could not open");
return -1;
}
printf("Reading file...\n");
get_row(file, 0, delim);
fclose(file);
return 0;
}
该程序不应编译,因为 file_name 未定义。
此外,在 getrow 函数内部,没有元素应该等于字段,而不是缓冲区的长度。逗号分隔符也不起作用,因为该字段中有逗号。
以下代码适用于 test.csv 文件
中的给定行
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
int get_row(FILE *file, int row_num, char delim[]) {
int n_line = 0;
int field = 0;
char row[BUFFER_SIZE], *line[BUFFER_SIZE];
while (fgets(row, BUFFER_SIZE, file)) {
if (n_line == row_num) {
char* element = strtok(row, delim);
while (element != NULL) {
if(strcmp(",", element) != 0 && strcmp("\n", element) != 0)
{
printf("%s\n", element);
line[field++] = strdup(element);
}
element = strtok(NULL, delim);
}
printf("Length of line %d is %d elements!\n", n_line, field);
return 0;
} else {
n_line++;
}
}
printf("There is no row %d in the file you selected.\n", row_num);
return 0;
}
int main(int argc, char **argv) {
FILE *file;
char delim[] = "\"";
char file_name[] = "test.csv";
file = fopen(file_name, "r");
if (!file) {
printf("Error: could not open %s\n", file_name);
return -1;
}
printf("Reading file...\n");
get_row(file, 0, delim);
fclose(file);
return 0;
}
在使用strtok 以逗号和引号(双引号)分隔后,我有时只能看到正确的元素数量计数。通常 printf 的长度为 0,但偶尔为 6 和 1,代码没有变化。
我试过只使用一个分隔符(逗号)并以不同方式定义 strtok 的标记输出,并重新排列 while 循环中语句的顺序,其中行的其他元素应该被分隔。这些是我用来测试代码的 .csv 文件的几行 (test.csv)。它与 NOAA 以 .csv 格式提供降水数据的格式相同。
"STATION","NAME","DATE","PRCP","PRCP_ATTRIBUTES"
"US183459384","XYZ ABC 9.0 E, WA US","2019-01-06","0.65",",,N"
"US183459384","XYZ ABC 9.0 E, WA US","2019-01-12","0.46",",,N"
"US183459384","XYZ ABC 9.0 E, WA US","2019-01-13","0.09",",,N"
"US183459384","XYZ ABC 9.0 E, WA US","2019-01-14","0.01",",,N"
"US183459384","XYZ ABC 9.0 E, WA US","2019-01-15","0.60",",,N"
"US183459384","XYZ ABC 9.0 E, WA US","2019-01-16","1.93",",,N"
下面是我的代码尝试。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
int get_row(FILE *file, int row_num, char delim[]) {
int n_line = 0;
int field = 0;
char row[BUFFER_SIZE], *line[BUFFER_SIZE];
while (fgets(row, BUFFER_SIZE, file)) {
if (n_line == row_num) {
printf("Length of line %d is %ld elements!\n", n_line, strlen(row));
char* element = strtok(row, delim);
while (element != NULL) {
printf("%s\n", element);
line[field++] = strdup(element);
element = strtok(NULL, delim);
}
return 0;
} else {
n_line++;
}
printf("There is no row %d in the file you selected.\n", row_num);
return 0;
}
int main(int argc, char **argv) {
FILE *file;
char delim[] = ", \"";
file = fopen(”test.csv”, "r");
if (!file) {
printf("Error: could not open %s\n", file_name);
return -1;
}
printf("Reading file...\n");
get_row(file, 0, delim);
fclose(file);
return 0;
}
我希望结果显示 5,但所有行的结果都是 0 或 1,偶尔显示 6。
程序中有许多语法错误。 这应该有效:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
int get_row(FILE *file, int row_num, char delim[]) {
char row[BUFFER_SIZE];
for (int i = 0; fgets(row, BUFFER_SIZE, file); i++) {
if (i == row_num) {
printf("Length of line %d is %ld elements!\n", i, strlen(row));
char* element = strtok(row, delim);
while (element) {
printf("%s\n", element);
element = strtok(NULL, delim);
}
break;
}
}
printf("There is no row %d in the file you selected.\n", row_num);
return 0;
}
int main() {
FILE *file;
char delim[] = ", \"";
file = fopen("test.csv", "r");
if (!file) {
puts("Error: could not open");
return -1;
}
printf("Reading file...\n");
get_row(file, 0, delim);
fclose(file);
return 0;
}
该程序不应编译,因为 file_name 未定义。 此外,在 getrow 函数内部,没有元素应该等于字段,而不是缓冲区的长度。逗号分隔符也不起作用,因为该字段中有逗号。 以下代码适用于 test.csv 文件
中的给定行#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
int get_row(FILE *file, int row_num, char delim[]) {
int n_line = 0;
int field = 0;
char row[BUFFER_SIZE], *line[BUFFER_SIZE];
while (fgets(row, BUFFER_SIZE, file)) {
if (n_line == row_num) {
char* element = strtok(row, delim);
while (element != NULL) {
if(strcmp(",", element) != 0 && strcmp("\n", element) != 0)
{
printf("%s\n", element);
line[field++] = strdup(element);
}
element = strtok(NULL, delim);
}
printf("Length of line %d is %d elements!\n", n_line, field);
return 0;
} else {
n_line++;
}
}
printf("There is no row %d in the file you selected.\n", row_num);
return 0;
}
int main(int argc, char **argv) {
FILE *file;
char delim[] = "\"";
char file_name[] = "test.csv";
file = fopen(file_name, "r");
if (!file) {
printf("Error: could not open %s\n", file_name);
return -1;
}
printf("Reading file...\n");
get_row(file, 0, delim);
fclose(file);
return 0;
}