Home c Move to a new line in the file (SI)

Move to a new line in the file (SI)




I need to write a character at the end of each line of the file. So how, after recording, go to the next line of the file?

Answer 1, authority 100%

In general, something like this is easiest:

# include & lt; stdlib.h & gt;
#include & lt; stdio.h & gt;
int main ()
  FILE * in = fopen ("data", "r");
  FILE * out = fopen ("data ~", "w");
  if (in == NULL || out == NULL)
    fprintf (stderr, "Error open file \ n");
    return 1;
  char addSym = '$';
  int c;
  while ((c = fgetc (in))! = EOF)
    if (c == '\ n') fputc (addSym, out);
    fputc (c, out);
  fclose (in);
  fclose (out);
  unlink ("data");
  rename ("data ~", "data");

Answer 2

If the text file already exists, then you read line by line by making a riplace "\ r \ n" to "{desired text} \ r \ n" and then save the text.


Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions