Thread: sscanf read error

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    7

    sscanf read error

    The man page of 'sscanf' says:

    "EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error"

    I am confused that what kind of read error may occur if sscanf is reading from a buffer on stack???

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    An empty string perhaps?
    Try some tests for yourself?
    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
    Registered User
    Join Date
    Jan 2009
    Posts
    7
    ok, Let me elaborate, i have a text file that contains lines like:

    ABCD 6 0
    EFGH 1 0

    and so on.....

    what i do is, i read each line using 'fgets()' in a char buffer say read_buff, apply a regular expression over it, that ensures the presence of two integers after the name, now by using sscanf i am reading the integers into integer variables, my question is that should i handle the read error specified in man pages, if yes, then please tell me the scenario in which it may occur....

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by me_ansh View Post
    ok, Let me elaborate, i have a text file that contains lines like:

    ABCD 6 0
    EFGH 1 0

    and so on.....

    what i do is, i read each line using 'fgets()' in a char buffer say read_buff, apply a regular expression over it, that ensures the presence of two integers after the name, now by using sscanf i am reading the integers into integer variables, my question is that should i handle the read error specified in man pages, if yes, then please tell me the scenario in which it may occur....
    you should check the return value of sscanf

    if it is less than number of items to be formatted - do something (probably ignore the line)

    so for you return value of -1 should not be different from return value of 0
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    7
    Ya, that is what i am asking...if i have ensured by my regular expression that integers are present, then is there a possiblity that sscanf may return error??, if yes, then is there a specific case that u may highlight ??

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by me_ansh View Post
    Ya, that is what i am asking...if i have ensured by my regular expression that integers are present, then is there a possiblity that sscanf may return error??, if yes, then is there a specific case that u may highlight ??
    Couldn't say without seeing the regular expression and sscanf format used

    PS. And I do not see a reason for using regular expressions while you could simply check the return value of sscanf (and maybe the first unparsed char if you interrested to known if really all the string was parsed)
    Last edited by vart; 01-28-2009 at 03:33 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suspect that in the case of sscanf(), it's UNLIKELY that EOF is returned, but some systems may be able to detect situations where it want's to give an error message back (e.g. validating the string storage and finding it invalid in some way).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM