无法正确计算C strtok后CSV中的元素



我有时只会看到我使用strtok通过逗号和引号划界的元素数量的正确计数(双引号(。通常,printf是长度为0,但偶尔为6和1,而没有更改代码。

我仅尝试使用一个定界符(逗号(,并以不同的方式定义strtok的令牌输出,并在据说划定行的其他元素的同时循环中重新安排语句顺序。这些是我用来测试代码(test.csv(的.csv文件的几行。NOAA的格式与a .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("%sn", 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 %sn", file_name);
        return -1;
    }
    printf("Reading file...n");
    get_row(file, 0, delim);
    fclose(file);
    return 0;
}

我希望结果显示5,但结果是所有行的0或1,有时为6。

此程序不应编译,因为未定义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("%sn", 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 %sn", file_name);
        return -1;
    }
    printf("Reading file...n");
    get_row(file, 0, delim);
    fclose(file);
    return 0;
}

程序中的许多语法错误。这应该有效:

#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("%sn", 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;
}

相关内容

  • 没有找到相关文章

最新更新