Problem with fscanf..

This is a discussion on Problem with fscanf.. within the C Programming forums, part of the General Programming Boards category; C Warning: Argument is incompatible with the corresponding format string conversion. fscanf(outfile, "%f", &out[i][j][k]); What is the issue here? The ...

  1. #1
    Unregistered
    Guest

    Problem with fscanf..

    C Warning:

    Argument is incompatible with the corresponding format string conversion.

    fscanf(outfile, "%f", &out[i][j][k]);


    What is the issue here? The syntax is correct... does this occur because out is passed into the function as a pointer already? If so how do I work around it. (Currently I get garbage out and the fscanf doesn't appear to be working when I do a dump of the out array within the subprogram)

    Here's more of the function code, the file is opened in the main program.

    int InitFile(FILE *outfile, float *out)

    --Declarations

    Loop 1{
    Loop 2 {
    Loop 3 {

    fscanf(outfile, "%f", &out[i][j][k]);
    }
    fscanf(outfile, "\n");
    }
    fscanf(outfile, "\n,\n");
    }


    I'm assuming the problem is because out is already declared as a pointer prior to the fscanf command, if I do the read in the main (w/o the pointer) program it seems to work correctly... however this piece of code will be used by many programs so I was hoping to make it a function so that it would be accessed in only one place if it needed to be edited.

    Any help you can provide would be greatly appreciated.

  2. #2
    Unregistered
    Guest
    >Argument is incompatible with the corresponding format string conversion.
    It means that you're trying to read a float but the data in the file is not a float. Double check the file to see that you're actually reading what you think you are.

  3. #3
    Unregistered
    Guest
    The data is indeed a float

    1.000 1.000 1.000 1.000
    1.000 1.000 1.000 1.000
    1.000 1.000 1.000 1.000
    1.000 1.000 1.000 1.000

    1.000 1.000 1.000 1.000
    1.000 1.000 1.000 1.000
    1.000 1.000 1.000 1.000
    1.000 1.000 1.000 1.000


    That is the contents of the test file it should be reading.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    But how is the 'out' array declared in main?

    Because
    int InitFile(FILE *outfile, float *out)
    is all wrong if it's a 3D array

  5. #5
    Unregistered
    Guest
    Oops the actual start of the file is:

    int InitFile (FILE *outfile, float *out[i][j][k])


    In main its declared:

    float out[i][j][k]

    and the function call is

    rstat = InitFile (&outfile, &out);

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Well that's the problem then

    Given the following array (where i, j, k are constants)
      float out[i][j][k];

    The function prototype is
      int InitFile (FILE *outfile, float out[i][j][k] )

    The call is
      FILE *outfile = fopen( "file.txt", "r" );
      rstat = InitFile ( outfile, out );

    And within the function, the file is read like so
      fscanf(outfile, "%f", &out[x][y][z] );

  7. #7
    Unregistered
    Guest
    Won't eliminating the pointer on the out variable, mean that I can't modify it in the function and still have the results available in the main?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    The results will be in the array declared in main

  9. #9
    Unregistered
    Guest
    Thanks I got it to work!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 4
    Last Post: 01-10-2006, 12:23 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. Replies: 12
    Last Post: 10-17-2005, 06:49 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21