Hey all, another question for you.
I have an input file consisting of three columns of double values representing x,y,z coordinates. I need to read them into an array. Now, my code works, but the compiler complains that I don't check the return value from fscanf - just a warning, the code still runs fine. But I tried to rewrite to check the return value anyway. It seems to read the values in fine, but when I go to reprint the array to check, all the values are 0. Any idea why?
Here is the working code. The printf both inside and outside the loop print the same thing.
Code:
for (i = 0; i < N_part; i++)
{
fscanf(particleList,"%lf%lf%lf", &particleMatrix[i][0], &particleMatrix[i][1], &particleMatrix[i][2]);
printf("%f\t%f\t%f\n",particleMatrix[i][0], particleMatrix[i][1], particleMatrix[i][2]);
if (particleMatrix[i][0] * particleMatrix[i][0] + particleMatrix[i][1] * particleMatrix[i][1] + particleMatrix[i][2] * particleMatrix[i][2] > maxRadiusSquared)
{
maxRadiusSquared = particleMatrix[i][0] * particleMatrix[i][0] + particleMatrix[i][1] * particleMatrix[i][1] + particleMatrix[i][2] * particleMatrix[i][2];
}
}
maxRadius = sqrt(maxRadiusSquared);//Radius of the cluster
for (i = 0; i < N_part; i++)
{
printf("%f\t%f\t%f\n",particleMatrix[i][0], particleMatrix[i][1], particleMatrix[i][2]);
}
Here is the nonworking code - the inside printf prints what it should, but the one outside prints 0's.
Code:
while (fscanf(particleList,"%lf%lf%lf", &particleMatrix[i][0], &particleMatrix[i][1], &particleMatrix[i][2]) != EOF)
{
maxRadiusSquared = particleMatrix[i][0] * particleMatrix[i][0] + particleMatrix[i][1] * particleMatrix[i][1] + particleMatrix[i][2] * particleMatrix[i][2];
printf("%f\t%f\t%f\n",particleMatrix[i][0], particleMatrix[i][1], particleMatrix[i][2]);
}
maxRadius = sqrt(maxRadiusSquared);
for (i = 0; i < N_part; i++)
{
printf("%f\t%f\t%f\n",particleMatrix[i][0], particleMatrix[i][1], particleMatrix[i][2]);
}
I am sure I am just using fscanf wrong in the second one, but how?