Thread: unknown number of chars

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > But, Salem (or anybody who can answer that), can you still explain my question about fgets and looping like that?

    > So, for every iteration, it's going to read a new line every time, right?
    NO!!!
    It will read the next 20 chars on the current line, and will keep doing that until it finds one of the buffers which contains the newline (in which case, fgets() will return before the buffer is full).

    It doesn't throw the rest of the line away just because it could not fit the line into the current buffer - it just returns a full buffer and hopes you will deal with that before calling fgets again.

    Newline is just a convenient place to stop.

    It's also not gets(), which will roam all through memory writing junk until it sees a newline, and nothing is going to get in its way until it sees that newline.

    And Quzah's code calls strlen with a NULL pointer.

    And Sebastiani's C++ code wont work with stdin.
    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.

  2. #17
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Oh, I always thought that fgets always incremented itself, not matter what. So, it depends on the \n char. If it reaches it, then it increments. Else, it'll just keep reading the same line. Okay, understood. Thanks, Salem.

    --Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 07:19 PM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM