I am writing an academic project in C and I can use only <fcntl.h>
and <unistd.h>
libraries to file operations.
I have the function to read file line by line. The algorithm is:
- Set pointer at the beginning of the file and get current position.
- Read data to the buffer (
char buf[100]
) with constant size, iterate character by character and detect end of line'\n'
. - Increment current position:
curr_pos = curr_pos + length_of_read_line;
- Set pointer to current position using
lseek(fd, current_position, SEEK_SET);
SEEK_SET
- set pointer to given offset from the beginning of the file. In my pseudo code current_position
is the offset.
And actually it works fine, but I always move the pointer starting at the beginning of the file - I use SEEK_SET - it isn't optimized.
lseek
accept also argument SEEK_CUR
- it's a current position. How can I move back pointer from current position of pointer (SEEK_CUR). I tried to set negative offset, but didn't work.