fscanf problem

Discussion in 'C Programming' started by jespersh@hotmail.com, Aug 28, 2005.

  1. Guest

    I have a problem concerning fscanf: I which to save an array of
    structures to a file, and to boil things down let's say I save the
    'type' member (which is a char) as:

    void save(struct part *ptr, const char *file, int npart){
    FILE *fout;
    int n;

    fout = fopen(file, "w");
    /* some check */

    for ( n=0; n<npart; n++ )
    fprintf(fout, "%c\n", ptr[n].type);

    fclose(fout);
    }

    (Here the array is 'npart' long.) Now, I wish to load the data through
    the following function:

    void load(struct part *ptr, const char *file, int npart){
    FILE *fin;
    int n;

    fin = fopen(file, "r");
    /* some check */

    for ( n=0; n<npart; n++ )
    fscanf(fin, "%c", &ptr[n].type);

    fclose(fin);
    }

    This will lead to very strange behavior. As I understand fscanf skips
    \n, space etc, but it seems from the output as if it includes the
    newline character twice.

    Am I missing the definition of fscanf?

    Jesper
    , Aug 28, 2005
    #1
    1. Advertising

  2. Guest

    Hrmmphh... I am not missing the definition... I meant "misunderstanding"
    , Aug 28, 2005
    #2
    1. Advertising

  3. In article <>,
    <> wrote:

    >I have a problem concerning fscanf:


    > fscanf(fin, "%c", &ptr[n].type);


    >This will lead to very strange behavior. As I understand fscanf skips
    >\n, space etc,


    There are two exceptions to fscanf skipping leading whitespace:
    - if the format element is %c
    - if the format element is %[

    --
    "Never install telephone wiring during a lightning storm." -- Linksys
    Walter Roberson, Aug 28, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Sourmail

    fscanf problem

    Thomas Sourmail, Jun 16, 2004, in forum: C Programming
    Replies:
    7
    Views:
    3,904
    Chris Torek
    Jun 17, 2004
  2. Kay

    fscanf and linked list problem

    Kay, Aug 29, 2004, in forum: C Programming
    Replies:
    7
    Views:
    1,913
  3. learner

    fscanf problem

    learner, Jun 27, 2005, in forum: C Programming
    Replies:
    5
    Views:
    342
    Michael Mair
    Jun 29, 2005
  4. rsk

    Problem in using fscanf

    rsk, Jul 30, 2007, in forum: C Programming
    Replies:
    10
    Views:
    638
    Peter 'Shaggy' Haywood
    Aug 3, 2007
  5. Problem with fscanf()....

    , Aug 31, 2007, in forum: C Programming
    Replies:
    11
    Views:
    677
    Keith Thompson
    Sep 1, 2007
Loading...

Share This Page