title: Add same text to end of each line in file from C
body: <p>I am trying to add a -1 to the end of each line of a file. For instance, file.txt is</p>
  
  <pre><code>1 4 5
  2 5 9
  3 5 6
  </code></pre>
  
  <p>but would become</p>
  
  <pre><code>1 4 5 -1
  2 5 9 -1
  3 5 6 -1
  </code></pre>
  
  <p>I am figuring out how to add text in general to a file from C, but I cannot figure out how to add the same text to each line in the file, and assure that the new line character is placed after the new last character in the lines (in this case -1).</p>
  
  <p>Here is what I have tried:</p>
  
  <pre><code>FILE *f = fopen("file.txt", "w");
  if (f == NULL)
  {
      printf("Error opening file!\n");
      exit(1);
  }
  
  /* print some text */
  const char *text = " -1";
  fprintf(f, "%s\n", text);
  </code></pre>
  
  <p>Any advice greatly appreciated!</p>