Re: How to lseek the larger file > 2GB under linux

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 <glynn@xxxxxxxxxxxxxxxxxx>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux