Reputation: 1573
How can I move the file position back x bytes in C?
I don't want to use fseek()
, because I'm reading lines of the file with open(fd, buf, buflength)
, not fopen()
.
I'd very much like to avoid reading the entire file into memory.
What I want to do is basically:
I know how to do everything except step 3. Anyone know how?
Upvotes: 0
Views: 2065
Reputation: 9618
According to http://rabbit.eng.miami.edu/info/functions/unixio.html you could use lseek:
int lseek(int fd, int position, int startpoint)
include: <unistd.h>
Sets the file position effective for the next read or write operation.
fd = file descriptor as returned by open
position = position within file:
number of bytes between startpoint and the desired position, may be negative.
startpoint = location in file that position is relative to, one of:
SEEK_SET Position is number of bytes from beginning of file
SEEK_END Position is number of bytes after end of file
SEEK_CUR Position is number of bytes after current position
returns <0 for error,
or resulting file position relative to beginning of file.
Moving beyond the end of the file is permitted; the file is extended in length
only if a write occurs beyond the current end of file. Moving before the
beginning of a file is not permitted. A write operation after a move only
overwrites the number of bytes actually written.
Upvotes: 2