fgets suddenly buggy behaviour (file stream damaged?)

Discussion in 'C++' started by jsmith, Jul 22, 2013.

  1. jsmith

    jsmith Guest

    Hi,
    what can cause a FILE* stream to no more read lines correctly
    using fgets() ? It seems the line end delimiter (\n) doesn't
    get evaluated correctly for _some_ lines.
    The line numbers where that error happens changes with each run.
    What can it be?
    Environment is XP in Virtual Env (vbox), VC++ 2008.
     
    jsmith, Jul 22, 2013
    #1
    1. Advertising

  2. jsmith

    Jack Applin Guest

    jsmith <> wrote:
    > what can cause a FILE* stream to no more read lines correctly
    > using fgets() ?


    Bad code, usually. Produce a minimal example of code that compiles
    and behaves incorrectly, and show it to us. Preparing the example
    will often result in you figuring out what you did wrong.
     
    Jack Applin, Jul 23, 2013
    #2
    1. Advertising

  3. jsmith

    Jorgen Grahn Guest

    On Tue, 2013-07-23, Jack Applin wrote:
    > jsmith <> wrote:
    >> what can cause a FILE* stream to no more read lines correctly
    >> using fgets() ?

    >
    > Bad code, usually.


    Specifically, fgets() is guaranteed to include the '\n' unless
    - EOF has been reached
    - the provided buffer is too short for the line of text

    Also note that std::getline(istream&, string&) can be a better
    alternative to C-style fgets() and FILE*.

    > Produce a minimal example of code that compiles
    > and behaves incorrectly, and show it to us. Preparing the example
    > will often result in you figuring out what you did wrong.


    Yes.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Jul 23, 2013
    #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. Replies:
    1
    Views:
    362
    =?Utf-8?B?QXVndXN0aW4gUHJhc2FubmE=?=
    Aug 2, 2006
  2. muthu
    Replies:
    4
    Views:
    439
    bruce barker
    Apr 24, 2007
  3. Sheth Raxit

    Qry : Behaviour of fgets -- ?

    Sheth Raxit, Sep 6, 2007, in forum: C Programming
    Replies:
    345
    Views:
    5,233
    Flash Gordon
    Oct 20, 2007
  4. Krumble Bunk

    fgets behaviour with strncmp

    Krumble Bunk, Jun 17, 2008, in forum: C Programming
    Replies:
    8
    Views:
    442
    santosh
    Jun 18, 2008
Loading...

Share This Page