fgets explanation

This is a discussion on fgets explanation within the C Programming forums, part of the General Programming Boards category; Can somebody help me understand how fgets works!?! I'm trying to read a notepad file and i dont understand what ...

  1. #1
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    fgets explanation

    Can somebody help me understand how fgets works!?!

    I'm trying to read a notepad file and i dont understand what exactly happens,for example if i have a file like this

    " why does the monkey never
    sit on his own tail and
    if he did what expression would
    he have on his face after "

    and i use an fgets() to read the info, will the fgets stop at the end of the line or will it
    continue to read as much of the file as its allowed?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    So long as the buffer is big enough, it reads exactly one line on each call.

    For example, this prints the file.
    while ( fgets( buff, sizeof buff, fp ) != NULL ) fputs( buff, stdout );

    If the buffer is smaller than a line, then it's filled with as many characters as will fit.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51
    thanks

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Quote Originally Posted by Salem
    If the buffer is smaller than a line, then it's filled with as many characters as will fit.
    As many characters that will fit with the attatched '\0' put at the end of the buffer.
    Just Clarifying.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    You know, reading one paragraph of the man page for fgets() tells you all this:
    char *fgets(char *s, int size, FILE *stream);

    fgets() reads in at most one less than size characters
    from stream and stores them into the buffer pointed to by
    s. Reading stops after an EOF or a newline. If a newline
    is read, it is stored into the buffer. A '\0' is stored
    after the last character in the buffer.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 02:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM

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