我需要从文件中读取三个变量的值,然后用它们来执行计算。 这些值以特定格式列出。例如,这些是一个这样的文件中的内容:从文件中读取值 - 有些是正确的,有些是不正确的
2 //number of items per variable
0 0 0 //values for center locations (stored as struct)
0 0 .5
10 //values for some variable v1 (type double)
5
-10 //values for some variable v2 (type double)
10
这是我的代码对这些数值阅读:
...
fscanf(file, "%d\n", &nItems);
for (unsigned int i = 0; i < nItems; i++)
{
float cx, cy, cz;
fscanf(file, "%f %f %f\n", &cx, &cy, &cz);
center[i].cx = cx;
center[i].cy = cy;
center[i].cz = cz;
}
for (unsigned int i = 0; i < nItems; i++)
{
fscanf(file, "%f\n", &v1[i]);
}
for (unsigned int i = 0; i < nItems; i++)
{
fscanf(file, "%f\n", &v2[i]);
}
我现在面临的问题是,当我在读这种方式输出它们的值,nItems的值和中心位置是正确的,但其余的都是不正确的。但是,这些值的符号和相对大小是正确的。例如,上面显示的值列表,这些都是输出值:
Correct Outputted values
2 2
0 0 0 0.000000 0.000000 0.000000
0 0 .5 0.000000 0.000000 0.500000
10 524288.000000
5 2048.000000
-10 -524288.000000
10 524288.000000
我不知道为什么在错误被读取的最后两个变量的值。我会很感激你的建议。
谢谢。
v1和v2的声明是什么?它们是%f的正确类型吗? – 2011-02-14 04:12:18