title: Why can't my program save a large amount (>2GB) to a file?
body: <p>I am having trouble trying to figure out why my program cannot save more than 2GB of data to a file. I cannot tell if this is a programming or environment (OS) problem. Here is my source code:</p>
  
  <pre><code>#define _LARGEFILE_SOURCE
  #define _LARGEFILE64_SOURCE
  #define _FILE_OFFSET_BITS 64
  #include &lt;math.h&gt;
  #include &lt;time.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  /*-------------------------------------*/
  //for file mapping in Linux
  #include&lt;fcntl.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;sys/stat.h&gt;
  #include&lt;sys/time.h&gt;
  #include&lt;sys/mman.h&gt;
  #include&lt;sys/types.h&gt;
  /*-------------------------------------*/
  #define PERMS 0600
  #define NEW(type) (type *) malloc(sizeof(type))
  #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
  
  void write_result(char *filename, char *data, long long length){
  
  int fd, fq;
  
    fd = open(filename, O_RDWR|O_CREAT|O_LARGEFILE, 0644);
  
    if (fd &lt; 0) {
    perror(filename);
    return -1;
    }
  
    if (ftruncate(fd, length) &lt; 0)
    {
    printf("[%d]-ftruncate64 error: %s/n", errno, strerror(errno));
    close(fd);
    return 0;
    }
  
    fq = write (fd, data,length);
  
    close(fd);
  
    return;
  
  }
  
  main()
  {
  long long offset = 3000000000; // 3GB
  char * ttt;
  ttt = (char *)malloc(sizeof(char) *offset);
  printf("length-&gt;%lld\n",strlen(ttt));  // length=0
  memset (ttt,1,offset);
  printf("length-&gt;%lld\n",strlen(ttt));  // length=3GB
  write_result("test.big",ttt,offset);
  return 1;
  }
  </code></pre>
  
  <p>According to my test, the program can generate a file large than 2GB and can allocate such large memory as well. 
  The weird thing happened when I tried to write data into the file. I checked the file and it is empty, which is supposed to be filled with 1.</p>
  
  <p>Can any one be kind and help me with this?</p>