I am trying to write 2GB to a file using pwrite, but my code below is writing a smaller amount.
However, if I write 2GB in total using 2 pwrite calls of 1GB, that works.
Expected file size: 2147483648 bytes (2GB), observed: 2147479552
Compiled as : gcc -Wall test.c -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -D_XOPEN_SOURCE=600
gcc v 4.5.0 on 64 bit Opensuse
Here is the complete program.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
size_t size = 2147483648; //2GB
off_t offset = 0;
int fd;
char *buf = (char*) malloc (size * sizeof(char));
if(buf == NULL)
{
printf("malloc error \n");
exit(-1);
}
if(-1 == (fd = open("/tmp/test.out", O_RDWR|O_CREAT, 0644)))
{
fprintf(stderr, "Error opening file. Exiting..\n");
free(buf);
exit(-1);
}
if(-1 == (pwrite(fd, buf, size, offset)))
{
perror("pwrite error");
free(buf);
exit(-1);
}
free(buf);
return 0;
}
From the pwrite man page:
Note that there’s no requirement for pwrite() to write the number of bytes you asked it to. It can write less, and this is not an error. Usually, you’d call pwrite() in a loop – if it doesn’t write all the data, or if it fails with errno==EINTR, then you call it again to write the rest of the data.