title: How to write string to file in different lines in C
body: <p>I have program that replaces a word in a file with another one, but in the new file the lines are all written as one line, not in different lines and paragraphs as required.</p>
  
  <p>I tried adding <code>'\n'</code> at the end of each line I am reading from the original file, but it is not working.</p>
  
  <p>This is my code:</p>
  
  <pre><code>int main() {
      FILE *f1, *f2;
      char word[MAX], fname[MAX];
      char s[MAX], replace[MAX];
      char temp[] = "temp.txt", *p1, *p2;
      printf("Enter your input file name:");
      fgets(fname, MAX, stdin);
      fname[strlen(fname) - 1] = '\0';
  
      scanf("%s", word);
  
      scanf("%s", replace);
  
      f1 = fopen(fname, "r");
      if (!f1) {
          printf("Unable to open the input file!!\n");
          return 0;
      }
      f2 = fopen(temp, "w");
      if (!f2) {
          printf("Unable to open temporary file!!\n");
          return 0;
      }
  
      while (fscanf(f1,"%[^\n]%*c", &amp;s) != EOF) {
          printf("%s",s); //I wanted to see what happens when I'm reading from the file. Previously I added at the end of string s the char '\n' but it didnt work
  
          if (strstr(s, word)) {
              p2 = s;
              while (p1 = strstr(p2, word)) {
                  while (p2 != p1) {
                      fputc(*p2, f2);
                      p2++;
                  }
                  p1 = p1 + strlen(word);
                  fprintf(f2, "%s", replace);
                  p2 = p1;
              }
              while (*p2 != '\0') {
                  fputc(*p2, f2);
                  p2++;
              }
          } else {
              fputs(s, f2);
          }
      }
  
      fclose(f1);
      fclose(f2);
  
      remove(fname);
  
      rename(temp, fname);
      return 0;
  }
  </code></pre>