[英]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.
通过使用printf,我可以确定程序在sscanf部分崩溃了。
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: 步骤1.变更:
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. 第2步。了解如何打开编译器警告(例如
gcc -Wall
),将其打开,并注意编译器告诉您的所有内容。 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.
这样可以为您节省很多时间,避免出现此特定错误,并且将来会继续为您节省很多时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.