Can';在C中CSV中使用strtok后的t抓取值

Can';在C中CSV中使用strtok后的t抓取值,c,csv,C,Csv,我试图从CSV文件中获取一个值,并对其求和,以查看它是否超过某个阈值。使用以下代码,我可以单独打印列的值,这样,如果CSV文件上的一行有“PTS,1,01/01/2020400.00”,它将打印 PTS 1 01/01/2020 400.00 但是,当我尝试更改它以便添加最后一个值时,它不起作用,即 while (token != NULL) { if (cnt == 4){

我试图从CSV文件中获取一个值,并对其求和,以查看它是否超过某个阈值。使用以下代码,我可以单独打印列的值,这样,如果CSV文件上的一行有“PTS,1,01/01/2020400.00”,它将打印

PTS
1
01/01/2020
400.00
但是,当我尝试更改它以便添加最后一个值时,它不起作用,即

while (token != NULL) {
                               if (cnt == 4){
                                int tmp = atof(token);
                                sum = sum + tmp;
                                printf("%lf\n", sum);
在我的程序中,它说增量在外部while循环中,所以我不明白为什么它不工作


PS对于不正确的缩进表示抱歉

一些提示:

  • 要使用fopen功能读取文件,请使用模式“r”而不是“a+”
  • 检查fopen是否成功,如果不成功则以错误代码终止是很有用的
  • total函数的参数可以是term和指向double(结果)的指针
  • 该函数可以返回一个bool,指示是否在.csv文件中找到该术语
  • 似乎是浮点类型,所以考虑使用加倍而不是int
  • 索引变量在C中通常以0开头
因此,如果将您的代码稍微修改到上述几点,它可能会如下所示:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>


bool total(char *term, double *sum) {
    char buf[100] = "";
    FILE *fp;
    if ((fp = fopen("record.csv", "r")) == NULL) {
        exit(1);
    }
    bool exist = false;
    *sum = 0;
    while (fgets(buf, sizeof(buf), fp)) {     /* read each line */
        if (strstr(buf, term)) {       /* test for term  */
            char *token = strtok(buf, ",");
            for (int cnt = 0; token != NULL; cnt++) {
                if (cnt == 3) {
                    double tmp = atof(token);
                    *sum = *sum + tmp;
                    printf("%lf\n", *sum);
                } else {
                    printf("%s\n", token);
                }
                token = strtok(NULL, ",");
            }
            exist = true;
        }
    }
    fclose(fp);
    return exist;
}

int main(void) {
    double sum = 0;
    char *term = "PTS";
    if (total("PTS", &sum)) {
        printf("result: sum containing term %s is %lf\n", term, sum);
    } else {
        printf("Error, no user record %s\n", term);
        exit(1);
    }
    return 0;
}
PTS
1
01/01/2020
400.000000
PTS
3
01/03/2020
600.450000
result: sum containing term PTS is 600.450000
调试控制台中的输出如下所示:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>


bool total(char *term, double *sum) {
    char buf[100] = "";
    FILE *fp;
    if ((fp = fopen("record.csv", "r")) == NULL) {
        exit(1);
    }
    bool exist = false;
    *sum = 0;
    while (fgets(buf, sizeof(buf), fp)) {     /* read each line */
        if (strstr(buf, term)) {       /* test for term  */
            char *token = strtok(buf, ",");
            for (int cnt = 0; token != NULL; cnt++) {
                if (cnt == 3) {
                    double tmp = atof(token);
                    *sum = *sum + tmp;
                    printf("%lf\n", *sum);
                } else {
                    printf("%s\n", token);
                }
                token = strtok(NULL, ",");
            }
            exist = true;
        }
    }
    fclose(fp);
    return exist;
}

int main(void) {
    double sum = 0;
    char *term = "PTS";
    if (total("PTS", &sum)) {
        printf("result: sum containing term %s is %lf\n", term, sum);
    } else {
        printf("Error, no user record %s\n", term);
        exit(1);
    }
    return 0;
}
PTS
1
01/01/2020
400.000000
PTS
3
01/03/2020
600.450000
result: sum containing term PTS is 600.450000

适当的缩进确实有助于查看循环结构。由于您的问题似乎围绕着对哪个循环包含特定语句的困惑,因此在不首先修复缩进的情况下寻求帮助似乎是不现实的。请更具体一些。“不起作用”的信息量不大。提供a、输入、预期输出和实际输出。“对不起,缩进不正确”。如果你意识到缩进是不正确的,那么为什么你不把它修好,让别人(和你)都能看清楚呢?在你这样做之后,这个问题可能会更加明显。