If the program is not intended to be read by the user, just make sure that you output it in the same form that it will be input. In this case, it may be easier to use a binary format.
Regardless, make sure you set your stream to be binary, otherwise Windows may expand \n into \r\n for text streams.
Alternately, one of the simplest ways to overcome this problem in a platform-agnostic way for text files that are intended to be user-read is to greedily consume all adjacent \r and \n characters as a single line ending, and you won't have to worry about how you output it.