The most efficient method is to read blocks or chunks of data into a buffer than scan the buffer.
The I/O has an overhead cost and the more data you can fetch per request, the better.
Searching in memory is always faster than reading one character at a time from the input.
Be aware of two cases:
- A line continues past the end of your buffer.
- The buffer is not filled up.
Edit 1: Memory Mapping
Another alternative is to have the OS treat the file as memory. Essentially, the OS will handle reading in chunks or blocks of the file as appropriate.
You will have to measure the performance (i.e. Profile), to find out which method is more efficient and by how much.
3
solved C++ read large text file [closed]