这是打开文件然后读取其内容的代码(位于函数内部)fscanf()
:
FILE *file = NULL;
int xTemp = 0, xTot = 0;
int yTemp = 0, yTot = 0;
int zTemp = 0, zTot = 0;
int i = 0;
file = fopen(nomeFile, "r");
if(file == NULL) {
return 0;
} else {
while(!feof(file)) {
if(fscanf(file, "%f %f %f", &xTemp, &yTemp, &zTemp) != 3) {
return -1;
} else {
i++;
xTot += xTemp;
yTot += yTemp;
zTot += zTemp;
}
}
coords.x = xTot/i;
coords.y = yTot/i;
coords.z = zTot/i;
return i;
}
这是我正在阅读的文件的内容fscanf()
:
3.5 2.1 -1.4
4.1 -4.1 2.9
2.6 2.5 3.2
-1.2 0.0 4.3
1.5 1.3 6.0
问题是它fscanf()
不会分配给xTemp
,yTemp
和zTemp
正确的值。