Line break does not work on file

Asked

Viewed 1,114 times

1

I take the elements of a list but at the time of saving it not saved with line break saves everything in the same line.

fprintf(arquivo, "%s\n", p->infoLinha);
  • It’s saving everything on the same line, or your text editor needs you to use the \r\n to separate lines? For example, if you open the generated file in the Wordpad (instead of Notepad), you see the separate lines?

  • Thanks needed the r n

2 answers

4

You have to open the file in text mode

arquivo = fopen(nome_arquivo, "w"); // modo sem "b"
fprintf(arquivo, "%s\n", info);

If, even so, the '\n' not converted to normal line break for your specific OS, open the file in binary mode and use "\r\n"

arquivo = fopen(nome_arquivo, "wb"); // modo com "b"
fprintf(arquivo, "%s\r\n", info);

Note that the first option makes the program work correctly in all environments (Windows, Linux, Mac, elevator controller, space shuttle, ...); the second option makes the program generate Windows files regardless of the environment in which it runs.
If you need to generate a Windows file on Linux, the best way is to create the file in Linux mode and convert it to Windows mode during the transfer process (FTP can do this automatically, for example)

0

I added the r and it worked

fprintf(arquivo, "%s\r\n", p->infoLinha);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.