简体   繁体   中英

Reading a file line delimited by a comma using fgets and sscanf

EDIT(Way more information): This part of the code initializes the structure. It is global.

struct csvVars
{
    int timeStamp[500];
    int xAccel[500];
    int yAccel[500];
    int zAccel[500];
    int xGyro[500];
    int yGyro[500];
    int zGyro[500];
};

struct csvVars *userVars;

This is my function which finds the users condition. From using the printf I can determine the program crashes at the sscanf part.

void userCondition()
{
    FILE *csvFile = fopen("C:\\user.csv", "r");
    if (csvFile == NULL)
    {
        printf("Error opening file ! \n");
        return;
    }


    char line[100];
    int i = 0, j;
    double average;
    double xAver = 0, yAver = 0, zAver = 0;


    //skips first line
    fgets (line, 100, csvFile);

    fgets (line, 100, csvFile);

    while(!feof(csvFile))
    {
        printf("111");
        sscanf(line, "%d,%d,%d,%d,%d,%d,%d", &userVars->timeStamp[i], &userVars->xAccel[i], &userVars->yAccel[i], &userVars->zAccel[i], &userVars->xGyro[i], &userVars->yGyro[i], &userVars->zGyro[i]);
        fgets (line, 100, csvFile);
        i++;
    }

    for (j = 0; j < i; j++)
    {
        xAver += userVars->xAccel[j];
        yAver += userVars->yAccel[j];
        zAver += userVars->zAccel[j];
    }

    xAver = xAver/(i+1);
    yAver = yAver/(i+1);
    zAver = zAver/(i+1);

    printf("%lf\n", xAver);
    printf("%lf\n", yAver);
    printf("%lf\n", zAver);

    fclose(csvFile);
}

This is a sample file line:

11201,2614,2441,2300,1854,978,1370

SOLUTION:

void userCondition()
{
    FILE *csvFile = fopen("C:\\user.csv", "r");
    char line[200];
    int i = 0, j;
    double xAver = 0, yAver = 0, zAver = 0;

    struct csvVars userVars;

    //skips first line
    fgets (line, 100, csvFile);

    fgets (line, 100, csvFile);

    while(!feof(csvFile))
    {
        sscanf(line, "%d,%d,%d,%d,%d,%d,%d", &userVars.timeStamp[i], &userVars.xAccel[i], &userVars.yAccel[i], &userVars.zAccel[i], &userVars.xGyro[i], &userVars.yGyro[i], &userVars.zGyro[i]);
        i++;
        fgets (line, 100, csvFile);
    }

    for (j = 0; j < i; j++)
    {
        xAver += userVars.xAccel[j];
        yAver += userVars.yAccel[j];
        zAver += userVars.zAccel[j];
    }

    xAver = xAver/(i+1);
    yAver = yAver/(i+1);
    zAver = zAver/(i+1);

    printf("%lf\n", xAver);
    printf("%lf\n", yAver);
    printf("%lf\n", zAver);

    fclose(csvFile);
}

Step 1. Change:

sscanf(line, "%d,%d,%d,%d,%d,%d,%d", userVars->timeStamp[i], userVars->xAccel[i], userVars->yAccel[i], userVars->zAccel[i], userVars->xGyro[i], userVars->yGyro[i], userVars->zGyro[i]);

to:

sscanf(line, "%d,%d,%d,%d,%d,%d,%d", &userVars->timeStamp[i], &userVars->xAccel[i], &userVars->yAccel[i], &userVars->zAccel[i], &userVars->xGyro[i], userVars->yGyro[i], &userVars->zGyro[i]);

Step 2. Find out how to turn on compiler warnings (eg gcc -Wall ), turn them on, and pay attention to anything the compiler tells you. Never compile without warnings enabled. This would have saved you a lot of time with this particular error, and will continue to save you a lot of time in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM