How to write explicitily \n or \r\n to file

I want to have the option to write ascii files using \n or \r\n depending the destiny.

If I use ofstream.write("\n",1) I get the same result as ofstream.write("\r\n",2) because c++ 'thinks' mmm you are on windows system, so I write \r\n by you....

Any idea ? Thanks
You can write in binary mode.
Is there another way ?
Thanks
I don't believe so. Streams automatically convert newline characters for you when you put them in normally as you are doing.
I am always surprised when someone asks how to do a thing and then rejects the answer given.

Yes, there are other ways, but they would be a learning curve to about ten levels above what you now know.

But, back to the original question, you assume something that is not true:
because c++ 'thinks' mmm you are on a windows system

If you are on a Windows system, then the C++ API will indeed translate the "\n" into a "\r\n".

If you are not on a Windows system, then the C++ API will do what is correct for your system. On Linux systems, for example, it will translate the "\n" into a "\n".

If you find that this is not the case, then there is something wrong with your build system -- and the only way it got that way is because you did something odd to it. Are you trying to cross-compile something, perchance? Are you playing with the stream traits system after your program begins?


The simple answer remains -- if you want binary (==no LF translations) control you must use binary output streams. It isn't even hard -- you just add a single flag to your stream's iosflags state when you open it.

Hope this helps.
Topic archived. No new replies allowed.