I'm trying to write a function that works the same as fgets() (reading a file/stream line-by-line) except using ReadFile. By doing so, I'm able to use it with CreateFile/LockFile/etc. and other similar WinAPI HANDLE-based functions to avoid having to additionally use fopen() and fgets().
I have come up with two methods so far, though I am not sure if either of the two are the ideal solution. I'm hoping someone on this forum can shed an idea/example or two.
Method 1: Reading the file char-by-char until the EOL (end of line)
- Pros: You are not reading any unnecessary data.
- Cons: Extremely slow, since you are reading only one character at a time.
- I have tested this method, and it proved to be quite slow.
Method 2: Reading a fixed buffer size until the EOL
- If the buffer has read more then the EOL position, I would then use SetFilePointer() to set move the current file pointer to after the EOL for the next call.
- Pros: Most likely faster (not sure).
- Cons: You are reading unnecessary data from the file since not every line would be the same as your fixed buffer.
- I have not tested this method yet.
Any comments or suggestions are greatly appreciated.