Thread: fgets without specifying size

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    35

    fgets without specifying size

    Is it possible to pull one line at a time from a stream without specifying the buffer size (unlike in fgets)? Also, is there any way to store that line into a char*? Thanks.

  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
    You write your own function which just calls fgetc() in a loop, and you keep allocating space with say malloc until you read the newline or the end of memory.
    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
    Sep 2006
    Posts
    35
    The only method I know of continually reallocates memory to a char* while adding the character to the last element. Personally, I want to avoid realloc if possible. Has anyone written a function which does this and wouldn't mind posting it?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Queue
    Has anyone written a function which does this and wouldn't mind posting it?
    std::string or std::vector? Or is C++ not an option?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The only method I know of continually reallocates memory to a char* while adding the character to the last element.
    Yep, that's about all there is to it.

    >Personally, I want to avoid realloc if possible.
    Then use malloc and strcpy. If you don't want to specify a size, you need to let your string grow, and that means dynamic allocation.

    >Has anyone written a function which does this and wouldn't mind posting it?
    Yes. In fact, this particular problem comes up regularly and several of our top minds come up with something different each time. I think I've posted a dozen or so variations of such a function over the years. Search is your friend.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    35
    Quote Originally Posted by Dave_Sinkula
    std::string or std::vector? Or is C++ not an option?
    Unfortunately, the reading must be in C to keep it consistent with the rest of the program. Prelude's post answered my question completely. Thanks everyone for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 06:14 PM
  3. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 02:50 PM
  4. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  5. Determining Data Size For Network Send/Rec :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 6
    Last Post: 05-07-2002, 09:01 PM