Thread: fscan skipping a character

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    fscan skipping a character

    I have the following:
    Code:
    while(fgetc(bFile) != EOF)
    {
       fscanf(bFile, "%s %lf %lf", custName, &totearn, &weekearn);
    }
    inside a text file i have:

    sarah 4.4 7.7
    john 3.3 8.8

    when i run:

    printf( "%s %lf %lf\n ", custName, totearn, weekearn);

    i get the following:

    arah 4.400000 7.700000
    john 3.300000 8.800000
    john 3.300000 8.800000



    Why is the first character in the text skipped ? and why is "john" printed twice?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Why is the first character in the text skipped ?
    Because you called fgetc

    > why is "john" printed twice?
    Because you didn't check for errors.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    >Because you called fgetc

    So that's a normal result when using fgetc? Why the first character and not another?

    >Because you didn't check for errors.

    I think it's a problem with how i'm calling fgetc. If i was calling it properly then maybe it wouldn't give me a duplicate record.

  4. #4
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Your first call of fgetc will get a character from the current line(sarah 4.4 7.7) which is 's'. Then the fscanf call will get the remaining chracters in the current line. Same with the seconds line

    First line: "sarah 4.4 7.7"
    Second line: "\njohn 3.3 8.8" //You have '\n' as your first charcter in this line thats why you output it completly. fgetc got '\n'.

    What about use fscanf in while() rather than fgetc? or use fgets then sscanf()

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You should not be calling fgetc like that. There are ways to deal with this, one is to use fgets to read a line of data from the file into a buffer and then to use sscanf to read from that buffer into your variables:

    Code:
    char buffer[100];
    while( fgets(buffer,sizeof(buffer),bFile) != NULL )
    {
        sscanf(buffer,"%s %f %f",custName,&totearn,&weekearn);
    
        ...
    
    }
    Last edited by hk_mp5kpdw; 10-17-2005 at 07:01 AM. Reason: Forgot a comma
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    hmm that doesn't seem to work. i've tried printing the weekearn(just to test) and i just gives me:

    nannannannannannannannannan

    possibly because, sscanf(buffer,"%s %f %f",custName,&totearn,&weekearn);

    isn't assigning the correct value.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you know, functions have return values for a reason. How about actually checking them once in a while?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > nannannannannannannannannan
    Like all good examples, it gives you a hint, not an answer.

    Use the %lf you originally had, since you're reading into double variables (not floats)

    hk_mp5kpdw shows how to read a file line by line, with none of this "where's the first character" or "why a double line" problem associated with various other hacky ways of reading files - eg feof() abuse.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  3. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM
  4. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  5. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM