I need to read a line of text (terminated by a newline) without making assumptions about the length. So I now face to possibilities:
fgets
and check each time if the last character is a newline and continuously append to a bufferfgetc
and occasionally realloc
the bufferIntuition tells me the fgetc
variant might be slower, but then again I don't see how fgets
can do it without examining every character (also my intuition isn't always that good). The lines are quite large so the performance is important.
I would like to know the pros and cons of each approach. Thank you in advance.
I suggest using fgets()
coupled with dynamic memory allocation - or you can investigate the interface to getline()
that is in the POSIX 2008 standard and available on more recent Linux machines. That does the memory allocation stuff for you. You need to keep tabs on the buffer length as well as its address - so you might even create yourself a structure to handle the information.
Although fgetc()
also works, it is marginally fiddlier - but only marginally so. Underneath the covers, it uses the same mechanisms as fgets()
. The internals may be able to exploit speedier operation - analogous to strchr()
- that are not available when you call fgetc()
directly.