title: How to create files with holes using lseek?
body: <p>I am learning, on how to create holes in files using lseek.</p>
  
  <p>This is the code that I have written thus far...</p>
  
  <pre><code>#include &lt;fcntl.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;string.h&gt;
  
  int main()
  {
      int fd;
      char name[20] = "Harry Potter";
  
      // Creating a file
      if( (fd = open( "book.txt", O_RDWR | O_CREAT , S_IWRITE | S_IREAD ) &lt; 0 )) {
          printf("\ncreat error");    
      }
  
      // Seeking 100th byte, from the begining of the file
      if ( lseek(fd, 100, SEEK_SET) == -1 ) {
          if (errno != 0) {
              perror("lseek");
          } 
      }
  
      // Writing to the 100th byte, thereby creating a hole
      if( write(fd, name, sizeof(char)*strlen(name)) != sizeof(char)*strlen(name) ) {
          if (errno != 0) {
              perror("write");
          }
      }
  
      // closing the file
      if ( close(fd) == -1 ) {
          if (errno != 0)
              perror("close"); 
      }
  
      return 0;
  }
  </code></pre>
  
  <p>and when I compile and execute this code I get an lseek error  and also the name 'Harry Potter' is not being inserted into the file. This is the output when I execute the above code :</p>
  
  <pre><code>lseek: Illegal seek
  Harry Potter
  </code></pre>
  
  <p>I am even trying to catch all errors.
  Kindly help me further.</p>