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,8,2,2
LK,1,2,2,7,0,0,0,0,8,0,0,0,2,2,12,2,2,24,0,0,2,2,8,2,2
LK,3,2,2,7,13,6,2,1,22,2,2,2,2,2,12,2,2,24,2,2,2,2,8,2,2
LK,1,2,2,7,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
LK,1,2,2,6,0,3,0,0,11,0,2,2,0,0,12,2,2,18,0,0,0,0,8,2,2
LK,1,2,2,7,13,6,2,1,16,2,1,1,2,2,12,2,2,6,2,2,2,2,8,2,2
LK,1,2,0,0,10,6,2,1,19,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
LK,1,2,2,5,13,6,2,1,12,1,0,0,0,0,12,2,2,6,0,0,0,0,8,2,2

我想忽略这段文字,只需要抓住数字。这是我的代码:
    int arr[rows][columns]; /* rows/columns of .csv file */
    char buf[1000];
    int r = 0;

    while (fgets(buf,1000,ifp)) {
        char read = 'N';
        const char *tok;
        int ret =0;
        int c = 0;
        int count = 0;
        char *ptr;
        printf("%s \n", buf);

        for (tok = strtok(buf, ","); tok && *tok; tok = strtok(NULL, ",\n")){
                printf("%s ", buf); /* replace buf with tok */
                if(isNumber(tok)==1){
                    read = 'Y';
                    ret = strtol(tok,&ptr,10);
                    arr[r][c] = ret;
                    c++;
                    printf("Entered ");
                    //printf("%ld ", arr[r][c]);
                }
                if(strtok(NULL,"\n") && read == 'N')
                    count++;
        }
        r++;
        //r -= count;
    }

isNumber本质上是isdigt的扩展,按预期工作。但是,每次为每个迭代打印tok时,它都会在第一个逗号之后停止。样本输出:Config LH LH LH LH LH LH LH LH LH LH LH LH LK LK LK LK LK LK LK LK LK LK LK LK。当它打印出.csv文件的每一行时,它似乎可以很好地使用fgets读取输入。所以看来问题在于我的for循环。看来我不正确地增加了我的代币。
尝试使用另一个示例:
    char alph[] = "a-b-c-e";
    for (const char *tok = strtok(alph, "-"); tok && *tok; tok = strtok(NULL, "-\0")){
        printf("%s ", tok);
    }

产量:
a b c e,这是正确的结果。
因此,我觉得我在解析我的文件时遗漏了一些东西。如有任何帮助,我们将不胜感激。谢谢您。

最佳答案

你的循环是:

 for (tok = strtok(buf, ","); tok && *tok; tok = strtok(NULL, ",\n"))
 {
     …omitted…
     if (strtok(NULL, "\n") && read == 'N')
         count++;
 }

您有三个到strtok()的呼叫,而if中的一个会吃掉所有到换行符的内容。我不知道你在想什么。看起来你应该省略这个strtok()-但是我不确定你需要用什么来替换它。
由于循环控件中的第二个调用最多读取换行符或逗号(strtok(NULL, ",\n")),因此除了将tok设置为NULL以指示不再有标记之外,您无法判断何时到达行的末尾。

09-25 20:49