Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7529703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:43:40+00:00 2026-05-30T04:43:40+00:00

I am trying to write 2GB to a file using pwrite, but my code

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:43:41+00:00Added an answer on May 30, 2026 at 4:43 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to write an object i made onto a file using Object output
I' trying to write an xml file from Excel VBA using Microsoft XML 6.0.
In trying to write more testable Java code, I have been using the Model-View-Presenter
Trying to write a GLOBAL CBT HOOK, this is My code, but my hooking
Im trying to write an algorithm using the algorithm package, but when I use
Trying to write a couple of functions that will encrypt or decrypt a file
Im trying to write a single byte at a certain location in a file.
I'm trying to write a blog post which includes a code segment inside a
Trying to write a windows speech recognition macro. Written using XML and scripting language
Trying to write a simple C# script to copy file to directory one level

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.