Reading a whole line without betting on size

i have been googling, and came to the conclusion that there is not standard C library (or commonly used) that reads a complete line of a file, without a size parameter being involved.

so, as a little exercise i decided to think it over, and make my own

i came up with an idea, and wanted to hear comments (keeping in mind i want to learn from this)

the idea, in pseudo bable would be like this

char * readholeline ( FILE *file, size_t chunk);
{
   while we dont hit the end of line
   {
      allocate a size of chunk array
      read chunk bytes into array
      add array into a new node of a linked list
   }
   calculate how many chars we have counting all nodes
   allocate memory for all chars
   while we still have have more nodes
   {
       copy string from node to "all chars string"
       next node  
       free previous node
   }
   
    return "all chars pointer"

}

i discarded readin one char at a time, because it would be i/o intensive, and added the chunk parameter, because most of the time we have "some idea" of how big a file is, so we can make a "close" guess, but not quite exact ...

im still writing the real code, but wanted to know what other would think, or if someone has a better approach too

thanks :b:

For line-based I/O, you want a maximum. Otherwise, when fed an endless line, your program will allocate endless amounts of memory. Make it a large maximum if you want, but give it some sort of maximum. Also note that while you can make your limit as large as you feasibly want, there are plenty of UNIX commandline tools, like standard sed and grep, that won't handle lines larger than 2048 bytes. Even things like uuencoding that translate raw binary into ASCII have measures to limit their line length because it's always in your best interest to keep line lengths reasonable.

Furthermore, reading more than one character at a time won't let you "unget" the extra data read beyond the end of line. You can unget one and only one character. So you have to read one-at-a-time if you want to keep all your data.

Also, your algorithm overcomplex, there's no need for a linked list or other data structure. You can resize already-allocated memory with realloc(). It's location may change, but already-defined data will stay defined, while the new area on the end will be undefined until you overwrite it.

I'd just use fgets() with a limit of a few kilobytes. If you really wanted to be sure, make the limit configurable with a commandline switch, just in case.

corona
thanks for the insight.
i didn't knew about other programs having a line limit, and neither tough of the "endless" line problem

about realloc, never crossed my mind, and it does surely sounds safer than keeping a list tight :stuck_out_tongue:

thanks for the info