title: C function to insert text at particular location in file without over-writing the existing text
body: <p>I have written a program which takes a file as input and whenever it finds a line with length > 80, it adds \ and \n to that file to make it 80 chars in width max.</p>
  
  <p>The problem is that I have used fseek to insert \ and \n whenever the length exceeds 80, so it overrides two characters of that line which exceeds length 80. Is there a way using which I can insert text without overriding the existing text? </p>
  
  <p>Here is my code:-</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  
  int main(int argc, char *argv[])
  {
    FILE *fp1,*fp2;
    int prev=0,now=0;
    char ch;
    int flag=0;
    long cur;
    fp1=fopen(argv[1],"r+");
    if(fp1==NULL){
      printf("Unable to open the file to read. Program will exit.");
      exit(0);
    }
    else{
      while((ch=fgetc(fp1))!=EOF){
        if(ch!=' ' &amp;&amp; ch!='\n'){
          now=now+1;
        }
        else{
          if(now&gt;=80){
              fseek(fp1,cur,SEEK_SET);
              fputc('\\',fp1);
              fputc('\n',fp1);
              now=0;
              continue;
          }
          if(ch=='\n'){
            flag=0;
            now=0;
            continue;
            }
          else{
            prev=now;
            cur=ftell(fp1);
          }
          now=now+1;
        }
      }
    }
    fclose(fp1);
    return 0;
  }
  </code></pre>
  
  <p>To run it, you need to do following:-</p>
  
  <pre><code>user@ubuntu$ cc xyz.c
  user@ubuntu$ ./a.out file_to_check.txt
  </code></pre>