title: Simple example for aio_write()
body: <p>I'm searching for a simple example for the <code>POSIX aio_write</code> function.</p>
  
  <h3>What I tried so far</h3>
  
  <p><em>The below is not too important. Just jump to answer</em></p>
  
  <p>The code below creates a file, but does not write anything to it. aio_error returns <code>22</code> (= <code>quota exceeded</code>, but there is enough space and r/w-permission on the drive).</p>
  
  <pre><code>#include &lt;aio.h&gt;
  #include &lt;stdio.h&gt;
  
  
  char CONTENT[] = "asdf;";
  const int LENGTH = 5;
  
  struct aiocb createIoRequest(int fd,  
                              off_t offset, 
                              volatile void * content, 
                              size_t length){
      struct aiocb ret; // &lt;-- not initialized. Will lead to an error ( see answer) 
      {
          ret.aio_fildes = fd;
          ret.aio_offset = offset;
          ret.aio_buf = content;
          ret.aio_nbytes = length;            
      }
      return ret;
  }
  
  
  int main(){
  
      FILE * file = fopen("outfile.txt","w");
      int fd = fileno(file);
      {    
          struct aiocb op  = createIoRequest(fd,0,CONTENT,LENGTH);        
  
          // schedule write
          // for valgrind mem leak output see comments from answer in
          //   https://stackoverflow.com/questions/4248720/aio-h-aio-read-and-write-memory-leak
          int ret = aio_write(&amp;op);
          printf("aio_write 1: %d\n",ret);
  
          // wait until everything is done
          {
              const struct aiocb * aiolist[1];
              aiolist[0] = &amp;op;
  
              int ret = aio_suspend(aiolist,1,NULL);
              printf("aio_suspend: %d\n",ret);
  
              // report possible errors
              {
                  ret = aio_error(&amp;op);
                  printf("errno 1: %d\n",ret);
              }
          }
      }
      fclose(file);
  
  
      return 0;
  }
  </code></pre>