Thread: fgets explanation

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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,218
    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, 06: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, 03: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