Strange behavior if I use fgets() after fscanf()

This is a discussion on Strange behavior if I use fgets() after fscanf() within the C Programming forums, part of the General Programming Boards category; Hi, If i trying reading a line of text using an fgets() command after reading the previous line with fscanf(), ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    26

    Exclamation Strange behavior if I use fgets() after fscanf()

    Hi,

    If i trying reading a line of text using an fgets() command after reading the previous line with fscanf(), fgets returns an empty line to me. And if I use another fgets() I get the line of text that I originally wanted to read. I have no issues if I just keep doing fgets one after the other..

    Is there some issue with fscanf() that causes this.. or is it just my compiler doing something funny.? And any ideas on how to get around this?

    thanks,

    Avinash

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    26
    Btw.. just in case its relevant, I am doing this on the Microsoft Visual C++ 2005 cl compiler. And for now, I got around it by reading the prev line with fgets and doing sscanf on it. But i would still like to know why this happens..

    Avinash

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    The fscanf call leaves the newline in the input buffer. The fgets reads the newline and thinks it's an empty line.
    "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

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    so the approach I would recomend - do not mix fscanf with fgets - read all lines with fgets

    and lines that need parsing - use sscanf on them
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You should read this entire quite short thread now all the way thru.

    Maybe start with post #2. It is about scanf functions and trapping the newline.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    26
    Hey,

    Thanks a lot for the quick replies guys. It makes sense now.. Both the methods work. 'using only fgets and sscanf on what I need to parse' and also 'Just adding a space at the end of the fscanf statement so that it reads in the newline too". Appreciate your help..

    Avinash

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read mix file: fscanf fgets
    By cfdprogrammer in forum C Programming
    Replies: 1
    Last Post: 03-20-2009, 11:38 AM
  2. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  3. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  4. Replies: 12
    Last Post: 10-17-2005, 06:49 AM
  5. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 02:35 AM

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