I want to use fread()
for IO for some reason (speed and ...). i have a file with different sized lines. by using a code like:
while( !EOF ){
fread(buffer,500MB,1,fileName);
// process buffer
}
the last line may read incompletely and we have to read the last line again in next trial, so how to force fread()
to continue from the beginning of the last line?
or if possible how to force fread()
to read more than 500MB untill reaching a \n or another specific character?
Thanks All
Ameer.
Assuming a buffer
of bytes that you have reverse found a \n
character in at position pos
, then you want to roll back to the length of the buffer minus this pos
. Call this step
.
You can use fseek
to move the file pointer back by this much:
int fseek( FILE *stream, long offset, int origin );
In your case
int ret = fseek(stream, -step, SEEK_END);
This will involve re-reading part of the file, and a fair bit of jumping around - the comments have suggested alternative ways that may be quicker.