无法通过strtok识别空白

时间:2019-06-13 07:22:50

标签: c csv

我是个菜鸟,所以为代码混乱表示歉意。

因此,我试图编写一个C程序,该程序将获取CSV文件并以字符串的形式读取每条记录行,将该字符串拆分为用于分隔数据的令牌,然后将字符串令牌数据转换为int或double或等等,然后将数据存储到结构中。

我遇到的问题是,我正在尝试编写是否检查当前令牌读取的内容是否为空的空白,这会导致它进入另一个条件,该条件用类似的东西填充该对应的struct字段, “没有记录数据”之类的。

void read_function(FILE *infile, FitbitData data[])
{
    int i = 0;
    //end of file loop
    while (!feof(infile))
    {

    //temporary string array to store the single line read
        char record[100];
        char *c_temp;
        char *temptemp;
        char *targetpatient = "12cx7";
        char *end;
        char *NA_string = "NA";

        fgets(record, 100, infile);
        //has white space and comma as delimters for strtok
        //reads first token which should be patient ID
        c_temp=strtok(record, " ,");





        //condtion checks if temporary c_temp token is equal to target patient
        //if condtion met it stores data
        //if not it skips to next record
        if (strcmp(c_temp, targetpatient) == 0)
        {
            /*since there is duplicate data in the csv file, c_temp which is the patient ID is stored to the 
            temptemp variable while c_temp moves onto next token which is the minute
            if the test to check to make sure the data is not duplicated is passed, then all data will be stored to struct*/
            temptemp = c_temp;

            //sets c_temp equal to new token which should be the minute recorded
            c_temp = strtok(NULL, " ,");
            //test for duplicate data
            if (strcmp(c_temp,data[i-1].minute)!=0)
            {

                strcpy(data[i].patient, temptemp);
                strcpy(data[i].minute, c_temp);
                //printf("%s", c_temp);

                //sets c_temp equal to new token which should be calories
                c_temp = strtok(NULL, " ,");
                //printf("%s\n", c_temp);
                //if check to make sure c_temp isn't empty data
                if (c_temp != '\0')
                {
                    data[i].calories = strtod(c_temp, &end);
                }

                else
                {
                    //need code that Fills the field with NA if there is white space for the token
                }

                //sets c_temp equal to new token which should be distance
                c_temp = strtok(NULL, " ,");
                //if check to make sure c_temp isn't empty data
                if (c_temp != '\0')
                {
                    data[i].distance = strtod(c_temp, &end);
                }

                else
                {
                    //need code that Fills the field with NA if there is white space for the token
                }

                //sets c_temp equal to new token which should be floors
                c_temp = strtok(NULL, " ,");
                //if check to make sure c_temp isn't empty data
                if (c_temp != '\0')
                {
                    data[i].floors = strtol(c_temp, &end, 10);
                }

                else
                {
                    //need code that Fills the field with NA if there is white space for the token
                }

                //sets c_temp equal to new token which should be heart
                c_temp = strtok(NULL, " ,");
                //if check to make sure c_temp isn't empty data
                if (c_temp != '\0')
                {
                    data[i].heartRate = strtol(c_temp, &end, 10);

                }

                else
                {
                    //need code that Fills the field with NA if there is white space for the token
                }

                //sets c_temp equal to new token which should be steps
                c_temp = strtok(NULL, " ,");
                //if check to make sure c_temp isn't empty data
                if (c_temp != '\0')
                {
                    data[i].steps = strtol(c_temp, &end, 10);               }

                else
                {
                    //need code that Fills the field with NA if there is white space for the token
                }

                //sets c_temp equal to new token which should be sleep
                c_temp = strtok(NULL, " ,");
                //if check to make sure c_temp isn't empty data
                if (c_temp != '\0')
                {
                    data[i].sleepLevel = strtol(c_temp, &end, 10);

                }

                else
                {
                    //need code that Fills the field with NA if there is white space for the token
                }


                //updates counter
                i++;
            }
        }


    printf("1st token:%s\n", data[0].patient);
    printf("2nd token: %s\n", data[0].minute);
    printf("3rd token %lf\n", data[0].calories);
    printf("4th token:%lf\n", data[0].distance);
    printf("5th token:%d\n", data[0].floors);
    printf("6th token:%d\n", data[0].heartRate);
    printf("7th token:%d\n", data[0].steps);
    printf("8th token:%d\n", data[0].sleepLevel);





    }

例如,如果我尝试将其作为CSV文件运行

12cx7,0:26:00,,,,,,69

我希望它能够识别那些空格,以便可以用NA填充它们。相反,当前函数在编译时会给我这些结果:

1st token:12cx7
2nd token: 0:26:00
3rd token 69.000000
4th token:-92559631349317830736831783200707727132248687965119994463780864.000000
5th token:-858993460
6th token:-858993460
7th token:-858993460
8th token:-858993460
Press any key to continue . . .

我有98%的把握确定问题的根源是我编写strtok的方式,并且以目前的形式,它只会跳过这些空白,直到找到不为空的令牌为止。我只是不知道如何重写它,所以不会。

0 个答案:

没有答案