0

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
3
  • show how data is stored in file Commented Mar 22, 2013 at 4:14
  • we need to see the whole code/what you input into your program. Commented Mar 22, 2013 at 4:14
  • Added requested information @555k Commented Mar 22, 2013 at 4:30

4 Answers 4

2

velocity is declared int but used like a double in your fscanf() and printf() calls. You probably meant to declare it as double, too.

Sign up to request clarification or add additional context in comments.

Comments

1

Edit: I posted this comment before the user posted the types of velocity and distance. I assumed velocity and distance were an incompatible type of float, not an integer.

Try this:

printf( "%2.3f      %2.3f", (float)velocity, (float)distance);

My guess is that you are passing variables into printf that are not the same size as floats, and so the second part of "velocity" is printed instead of distance. Printf uses the stack to pass variables and the number and size of variables are not defined by the function, so any size mismatches will cause problems like this.

2 Comments

the float cast gives me 0's for velocity. @c.fogelklou 0.000 0.032 0.000 0.034 0.000 0.214 0.000 0.263 0.000 0.275 0.000 0.275 0.000 0.450
You get 0.000 for velocity because it is originally an integer in your source code. You didn't post this at first so I assumed it was a type of float. You also need velocity to be a float type, otherwise it can't hold values between 0 and 1.
0

Declare objectIdNum and velocity as double so that fscanf and printf will work correctly.

you declared variable velocity as int and is using wrong format specifier in fscanf and in printf

Comments

0

you need 3 changes

1) Declare velocity as double.

2) in fscanf, objectIdNum should be read as %d not %lf.

finally use flose(tempFileIn); before returning from main(exiting the program).

note: i assume velocity as double because practically velocity is a real number and not a integral value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.