Reputation:
I need to read a text file which may contain long lines of text. I am thinking of the best way to do this. Considering efficiency, even though I am doing this in C++, I would still choose C library functions to do the IO.
Because I don't know how long a line is, potentially really really long, I don't want to allocate a large array and then use fgets
to read a line. On the other hand, I do need to know where each line ends. One use case of such is to count the words/chars in each line. I could allocate a small array and use fgets
to read, and then determine whether there is \r,
\n
, or \r\n
appearing in the line to tell whether a full line has been read. But this involves a lot of strstr
calls (for \r\n
, or there are better ways? for example from the return value of fgets?). I could also do fgetc
to read each individual char one at a time. But does this function have buffering?
Please suggest compare these or other different ways of doing this task.
Upvotes: 2
Views: 969
Reputation: 5532
The correct way to do I/O depends on what you're going to do with the data. If you're counting words, line-based input doesn't make much sense. A more natural approach is to use fgetc and deal with a character at a time and let stdio worry about the buffering. Only if you need the whole line in memory at the same time to process it should you actually allocate a buffer big enough to contain it all.
Upvotes: 2