0

I'm currently working on a file that reads this information from a .txt file

Alpha 300 458 760
Gamma 197 222 435
Delta 666 777 888
Epsilon 123 456 77
Phi 515 120 98

From there I have to calculate the distance from 0,0,0 of these points. I then must put the Name and Distance into another file and print the name, x, y, z, and distance on the console. My problem occurs when I try to fscanf to retrieve the date from the the .txt file but the only output I get is:

Name             X               Y               Z               Distance
Alpha    0.000000        0.000000        0.000000        0.000000
Gamma    0.000000        0.000000        0.000000        0.000000
Delta    0.000000        0.000000        0.000000        0.000000
Epsilon  0.000000        0.000000        0.000000        0.000000
Phi      0.000000        0.000000        0.000000        0.000000

It seems like it is only reading the first string but not the following numbers. Please help.

int main(void)
{
//Variables
double xCord,yCord,zCord, distanceOfObj = 0.0;
char locationName[NAMELEN];

//Open Location file and add pointer
FILE *locationFile = fopen ("location.txt","r");

//Open distance file and add pointer
FILE *distanceFile = fopen ("distance.txt","w");

//Header for screen and distance file
fprintf(distanceFile, "Name \t\t Distance \t\n");
printf ("Name \t\t X \t\t Y \t\t Z \t\t Distance \n");

//Extract information
while (!feof(locationFile))
{
    fscanf(locationFile, "%s %lf %lf %lf", &locationName, &xCord, &yCord, &zCord);
    distanceOfObj = calcDistance(xCord, yCord, zCord);
    fprintf(distanceFile, "%s\t\t %lf\n", &locationName, &distanceOfObj);
    printf ("%s\t %lf\t %lf\t %lf\t %lf\n", &locationName, &xCord, &yCord, &zCord, &distanceOfObj);
}

//Close files
fclose(locationFile);
fclose(distanceFile);`

}

0 Answers0