Problem in using fscanf

Discussion in 'C Programming' started by rsk, Jul 30, 2007.

  1. rsk

    rsk Guest

    Hi Friends,

    I have written a code which suppose to read all the numbers from a hex
    file,But to my surprise the code is skiping every alternate value.Don't
    know why?

    Can you please help me in solving this problem.

    The code is as follows;


    main ()
    {
    FILE *fdataptr;
    int x;
    int ctrl;
    x=1;

    // Open the file containing data
    fdataptr = fopen("data.txt", "r");
    if(fdataptr==NULL){
    printf("\n Cannot open File data.txt");
    exit(1);
    }

    do {
    fscanf(fdataptr,"%x", &ctrl);
    printf ("\n\n data is 0x%x \n\n",ctrl);
    if (fscanf(fdataptr,"%x", &ctrl)==EOF)
    break;
    } while (x);

    }


    data.txt is having the values

    11111111
    22222222
    33333333
    44444444
    55555555
    66666666
    77777777
    88888888
    99999999




    Thanks & Regards,
    RSK...
    rsk, Jul 30, 2007
    #1
    1. Advertising

  2. rsk

    Mark Bluemel Guest

    rsk wrote:
    > Hi Friends,
    >
    > I have written a code which suppose to read all the numbers from a hex
    > file,But to my surprise the code is skiping every alternate value.Don't
    > know why?


    Because you read and ignore every second number?
    [snip]

    > do {
    > fscanf(fdataptr,"%x", &ctrl);
    > printf ("\n\n data is 0x%x \n\n",ctrl);
    > if (fscanf(fdataptr,"%x", &ctrl)==EOF)
    > break;
    > } while (x);
    Mark Bluemel, Jul 30, 2007
    #2
    1. Advertising

  3. rsk

    rsk Guest

    Can you correct me to print all the data values
    rsk, Jul 30, 2007
    #3
  4. rsk

    Mark Bluemel Guest

    rsk wrote:
    > Can you correct me to print all the data values


    Please quote some context when you are replying.

    I'm not going to correct your program for you. I've already told you
    what you are doing wrong, and I think it's now down to you to correct it.
    Mark Bluemel, Jul 30, 2007
    #4
  5. rsk

    Richard Guest

    "rsk" <> writes:

    > Can you correct me to print all the data values
    >


    Look how many times you are calling fscanf. Hint : too many times in the
    loop.

    Aha!
    Richard, Jul 30, 2007
    #5
  6. rsk

    rsk Guest

    Can you correct me to print all the data values
    rsk, Jul 30, 2007
    #6
  7. On Jul 30, 10:13 am, "rsk" <> wrote:
    > Can you correct me to print all the data values


    if (fscanf(fdataptr,"%x", &ctrl)==EOF)

    should be

    if (ctrl==EOF)
    Benoit Lefebvre, Jul 30, 2007
    #7
  8. Benoit Lefebvre wrote:
    > On Jul 30, 10:13 am, "rsk" <> wrote:
    >> Can you correct me to print all the data values

    >
    > if (fscanf(fdataptr,"%x", &ctrl)==EOF)
    >
    > should be
    >
    > if (ctrl==EOF)


    No, it shouldn't be that. If fscanf doesn't read ctrl, it doesn't set ctrl;
    ctrl isn't somehow set to EOF.
    Harald van =?UTF-8?B?RMSzaw==?=, Jul 30, 2007
    #8
  9. Benoit Lefebvre said:

    > On Jul 30, 10:13 am, "rsk" <> wrote:
    >> Can you correct me to print all the data values

    >
    > if (fscanf(fdataptr,"%x", &ctrl)==EOF)
    >
    > should be
    >
    > if (ctrl==EOF)


    No, it shouldn't.

    The proper solution is to structure the loop more intelligently:

    while(fscanf(fdataptr,"%x", &ctrl) == 1)
    {
    printf ("\n\n data is 0x%x \n\n",ctrl);
    }

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Jul 30, 2007
    #9
  10. rsk

    John Gordon Guest

    In <> "rsk" <> writes:

    > do {
    > fscanf(fdataptr,"%x", &ctrl);
    > printf ("\n\n data is 0x%x \n\n",ctrl);
    > if (fscanf(fdataptr,"%x", &ctrl)==EOF)
    > break;
    > } while (x);


    This loop calls fscanf() twice. You're only printing the result from the
    first call. The results from the second fscanf() are discarded.

    Change your loop to only call fscanf() once.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Jul 30, 2007
    #10
  11. Groovy hepcat rsk was jivin' in comp.lang.c on Mon, 30 Jul 2007 11:20
    pm. It's a cool scene! Dig it.

    > I have written a code which suppose to read all the numbers from a hex
    > file,But to my surprise the code is skiping every alternate
    > value.Don't know why?
    >
    > Can you please help me in solving this problem.
    >
    > The code is as follows;


    The code you posted will not even compile. You need to include some
    headers. You need stdio.h for printf(), fscanf() and fopen() as well as
    stdlib.h for exit() (and EXIT_SUCCESS and EXIT_FAILURE - see below).

    #include <stdio.h>
    #include <stdlib.h>

    > main ()


    int main(void)

    is better.

    > {
    > FILE *fdataptr;
    > int x;
    > int ctrl;
    > x=1;
    >
    > // Open the file containing data
    > fdataptr = fopen("data.txt", "r");
    > if(fdataptr==NULL){
    > printf("\n Cannot open File data.txt");


    Error messages typically go to the standard error stream (stderr).
    Also, since stderr is typically line buffered (and stdout is usually
    fully buffered or line buffered), you should end the output with a
    newline character. There is no need to begin the output with a newline.

    frintf(stderr, "Cannot open File data.txt\n");

    > exit(1);


    Returning would be better (IMHO) than calling exit() from main(). In
    either case, portable exit values are 0, EXIT_SUCCESS and EXIT_FAILURE.

    return EXIT_FAILURE;
    > }
    >
    > do {
    > fscanf(fdataptr,"%x", &ctrl);
    > printf ("\n\n data is 0x%x \n\n",ctrl);


    Sure you've got enough newlines there? Your output is going to take up
    alot of vertical space, with many large gaps. It's just a suggestion,
    but wouldn't it be better to output fewer blank lines? Feel free to
    ignore this if you wish.

    printf("data is 0x%x\n", ctrl);

    > if (fscanf(fdataptr,"%x", &ctrl)==EOF)
    > break;


    Other people have already told you you're reading two numbers in this
    loop. However, what they failed to tell you is that this is a very poor
    way to control a loop.

    > } while (x);


    The value of x is never changed. The only way to end this loop,
    therefore, is to break out of it. Very poor! A much better loop would
    be the following:

    while(1 == fscanf(fdataptr,"%x", &ctrl))
    {
    printf("data is 0x%x\n", ctrl);
    }

    This uses the return value of fscanf() to control the loop. This is as
    it should be, since you want to enter the loop body as long as you can
    read a number. You can also do away with the unused variable x.
    Now, since this is the end of a function returning int, it's a good
    idea to return an int here. (See above for portable exit values.)

    return 0;
    > }


    --
    Dig the sig!

    ----------- Peter 'Shaggy' Haywood ------------
    Ain't I'm a dawg!!
    Peter 'Shaggy' Haywood, Aug 3, 2007
    #11
    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,892
    Chris Torek
    Jun 17, 2004
  2. Kay

    fscanf and linked list problem

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

    fscanf problem

    learner, Jun 27, 2005, in forum: C Programming
    Replies:
    5
    Views:
    337
    Michael Mair
    Jun 29, 2005
  4. fscanf problem

    , Aug 28, 2005, in forum: C Programming
    Replies:
    2
    Views:
    455
    Walter Roberson
    Aug 28, 2005
  5. Replies:
    7
    Views:
    460
    Default User
    Sep 5, 2006
Loading...

Share This Page