gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > SOLVED: fprintf and \n

#153616 - Rajveer - Wed Apr 02, 2008 10:02 pm

How do I create a new line if I'm writing to a file with fprintf? \n doesn't work and I can't find anything on Google (the file gets written but without the new lines).

Code:
FILE *file_Pointer;
file_Pointer = fopen(/game_Directory/settings.ini,"w");

fprintf(file_Pointer,"[KEYS]\n");
fprintf(file_Pointer,"\n");
fprintf(file_Pointer,"KEY_A %d\n", playerKeys.A);
fprintf(file_Pointer,"KEY_B %d\n", playerKeys.B);
...

fclose(file_Pointer);


Last edited by Rajveer on Wed Apr 02, 2008 10:50 pm; edited 1 time in total

#153618 - Rajveer - Wed Apr 02, 2008 10:49 pm

Scratch that, \n doesn't work with notepad but does with wordpad. Incase anyone else ever has this "problem", I used \r\n instead of \n, \r is the ASCII Line Feed character. Most software that handles plain ASCII text files can handle combinations of \r and \n.

#162800 - mml - Sat Sep 13, 2008 9:53 am

\r\n is a windows-style line ending; \n is unix and mac os x style; \r is mac os 9 and earlier style.

The C library for each system is smart enough to treat \n alone as the appropriate line ending for that system, meaning for code portability you generally stick with just \n alone and let the C library figure it out. For files you're transferring between a couple of systems (say, between windows and your DS) it can get a little hairier as you've discovered.

Notepad only handles windows-style line endings; wordpad is a bit smarter and handles the others too.

Edit: just saw the date on this thread. Doh!