I'm having some issues with fscanf. I'm very new to C but I cannot seem to get fscanf to load the correct information from a .txt file.
int main() {
//Vars
FILE *tempFileIn;
int rowIndex = 0;
int objectIdNum;
double magnitudePhotographed;
double distance;
int velocity;
double magnitudeCorrected;
double magnitudeTotal;
//Read File Data
tempFileIn = fopen("hubbleData.txt","r");
if (tempFileIn == NULL) {
printf("File read error.");
}
printHeaders();
while(!feof(tempFileIn)) {
fscanf(tempFileIn, "%lf %lf %lf %lf %lf", &objectIdNum, &distance, &velocity, &magnitudeCorrected, &magnitudeTotal);
printf( "%2.3f %2.3f", velocity, distance);
printf("\n");
rowIndex++;
}
return 0;
}
The velocity is getting printed as expected but distance is always printed as 0.0000. If I switch the print order of the two, distance will be printed correctly and the velocity will be printed as 0.0000. I'm only concerned about the second and third columns but have to scan all of them as per the project guidelines.
Input format:
1 0.032 +170 1.5 -16.0
2 0.034 +290 0.5 17.2
6822 0.214 -130 9.0 12.7
598 0.263 -70 7.0 15.1
221 0.275 -185 8.8 13.4
224 0.275 -220 5.0 17.2
5457 0.45 +200 9.9 13.3
Actual Output:
170.000 0.000
290.000 0.000
-130.000 0.000
-70.000 0.000
-185.000 0.000
-220.000 0.000
200.000 0.000
Expected Output:
170.000 0.032
290.000 0.034
-130.000 0.214
-70.000 0.263
-185.000 0.275
-220.000 0.275
200.000 0.45