#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>  // lseek

int main()
{
  int fd;
  off_t size;

  int n_read, m_written;
  char buf[12]="HELLO WORLD!";
  
  fd = open("/tmp/foo", O_RDWR);
  //    fd = open("/tmp/foo", O_RDWR | O_APPEND);
  
  /* check if open was successful */
  
  if (fd == -1) {  /* open failed: print error message and exit */
    perror("Error in 'open'");
    exit(1);
  }
  
  size = lseek(fd, 5, SEEK_CUR);
  
  /* check if open was successful */

  if (size == -1)
    {
      perror("Error in 'lseek'");
      exit(1);
    }
  
  m_written = write(fd,buf,sizeof(buf));   // write to the file

  if (m_written == -1) {
    perror("Error in 'write'\n");
    exit(1);
  }
  
  close(fd);

  exit(0);
}
