Re: How to lseek the larger file > 2GB under linux
Glynn Clements <[email protected]>
| Newsgroups | org.kernel.vger.linux-c-programming |
|---|---|
| Message-ID | <[email protected]> |
Nicle wrote:
> I have a file > 2GB, and my job is seeking the file to pos: 2.1G.
> But, it seems that the lseek64 doesn't work.
> Here is the sample code:
[snip]
The following complete program works fine for me:
----------------------------------------------------------------------
#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FILENAME "test.dat"
int main(void)
{
int fd = -1;
long long pos = (long long) 2*1024*1024*1024 + 10;
fd = open(FILENAME, O_CREAT|O_WRONLY|O_LARGEFILE, 0666);
if (fd < 0) {
perror("open");
return 1;
}
if (lseek64(fd, pos, SEEK_SET) < 0) {
fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
return 1;
}
return 0;
}
----------------------------------------------------------------------
In general, you should post complete programs which can be compiled
without needing to "fill in the blanks", as the problem may lie in
something which was omitted.
> The return val of lseek64 was "<0", but the strerror told me "Success".
The return value is an off64_t; ensure that it isn't being truncated
to an int, because that will result in a negative value.
In the above, the 0 on the RHS of the comparison should be promoted to
an off64_t automatically (if it isn't, that's a bug in the compiler).
--
Glynn Clements <[email protected]>