title: Using lseek and write lead to input lines getting appended
body: <p>I am trying to write into a file line by line, but I am not able to. It is getting appended in the same line. Please help.</p>
  
  <p>Here is the sample code:</p>
  
  <pre><code>int main()
   {
       long filesize;
       char sentence[1000];
       int count = 5;
       int m_iSourceFileData;
       int i = 0;
       long offset =0;
  
      if ((m_iSourceFileData = open("test.txt", O_RDWR | O_APPEND | O_CREAT,S_IWRITE | S_IREAD)) != -1)
       {
           while(i != count)
           {
               if ((lseek(m_iSourceFileData, offset, SEEK_SET)) != -1)
               {
                   printf("Enter a sentence: %d\n", i);
                   gets(sentence);
                   filesize = strlen(sentence);
                   write(m_iSourceFileData, sentence, filesize);
                   offset += filesize;
                   printf("Offset is %ld, filesize:%ld\n",offset,filesize);
                   i++;
               }
  
           }
  
           if(m_iSourceFileData != -1)
           {
               close(m_iSourceFileData);
               m_iSourceFileData = -1;
           }
       }
  }
  </code></pre>
  
  <p><strong>Input</strong></p>
  
  <pre class="lang-none prettyprint-override"><code>$ a.out
  Enter a sentence: 0
  write
  Offset is 5, filesize:5
  Enter a sentence: 1
  into
  Offset is 9, filesize:4
  Enter a sentence: 2
  file
  Offset is 13, filesize:4
  Enter a sentence: 3
  line after
  Offset is 23, filesize:10
  Enter a sentence: 4
  line
  Offset is 27, filesize:4
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre class="lang-none prettyprint-override"><code>writeintofileline afterline
  </code></pre>