Hi,
Can I seek a file using 2 lseek() at the same time, each one seeks for different thing?
Printable View
Hi,
Can I seek a file using 2 lseek() at the same time, each one seeks for different thing?
Nothing happens in a single process "at the same time". If they're separate processes then it makes no difference because each has its own internal offset pointer. You'll have to be more specific about what you're actually doing.
No, you can't. The second lseek() will start where the first left off.
You could always do something like this:
Code:#include <stdio.h>
void showline(FILE *fp, long *offset)
{
char buf[BUFSIZ];
fseek(fp, *offset, SEEK_SET);
fgets(buf, sizeof(buf), fp);
fputs(buf, stdout);
*offset = ftell(fp);
}
int main(void)
{
FILE *fp;
long offsets[2];
int i;
if(!(fp = fopen("flipflop.txt", "r")))
{
puts("Unable to open 'flipflop.txt' for reading!");
return 1;
}
offsets[0] = 0;
offsets[1] = 50;
for(i = 0;i < 5;++i)
{
showline(fp, &offsets[0]);
showline(fp, &offsets[1]);
}
fclose(fp);
return 0;
}
Code:itsme@itsme:~/C$ cat flipflop.txt
000000000
111111111
222222222
333333333
444444444
555555555
666666666
777777777
888888888
999999999
itsme@itsme:~/C$
That's using standard streams, but the concept is the same for low-level I/O.Code:itsme@itsme:~/C$ ./flipflop
000000000
555555555
111111111
666666666
222222222
777777777
333333333
888888888
444444444
999999999
itsme@itsme:~/C$