Line break does not work on file

1

I get the elements of a list but at the time of saving it does not save with line break saves everything on the same line.

fprintf(arquivo, "%s\n", p->infoLinha);
    
asked by anonymous 04.02.2016 / 19:54

2 answers

4

You have to open the text-mode file

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

If, even then, the '\n' is not converted to the normal line break for your specific OS, it opens the file in binary mode and uses "\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 causes the program to generate Windows files regardless of the environment in which it runs. If it is necessary to generate a Windows file in 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)

    
04.02.2016 / 22:51
0

I added the \ r and it worked

fprintf(arquivo, "%s\r\n", p->infoLinha);
    
04.02.2016 / 20:09