Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to write 2GB to file, seeing incorrect amount being written

Tags:

c

posix

file-io

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;
}
like image 852
jitihsk Avatar asked Jan 18 '23 10:01

jitihsk


1 Answers

From the pwrite man page:

Description

pwrite() writes up to count bytes from the buffer starting at buf to the file descriptor fd at offset offset. The file offset is not changed.

Return Value

On success, the number of bytes written is returned (zero indicates that nothing was written), or -1 on error, in which case errno is set to indicate the error.

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.

like image 194
user9876 Avatar answered Feb 15 '23 09:02

user9876