I'm working with a basic std::ofstream
object, created as follows:
output_stream = std::ofstream(output_file.c_str());
This creates a file, where some information is put in. Let me show an example of such a message: (Watch window excerpt)
full_Message "Error while processing message:\r\n\tForecast Request:"
All this is ok, but after having launched following commands, there is a problem:
output_stream << full_Message;
output_stream.flush();
In order to see what is wrong, let's look at the hexadecimal dump of the file: (this is a hexadecimal display of the file, as seen in Notepad++. For clarity reasons I've taken a screenshot.)
As you can see, the character 0d
is doubled, resulting in following display:
Error while processing message:
Forecast Request:
(There's a newline too much, both lines should be directly one after the other)
I am aware of the addition of #13
characters while doing file conversion from UNIX/Linux to Windows, but this is not relevant here: I'm purely working with a Windows file, on a Windows system, so there should be no need to add any #13
character.
Does anybody have an idea how I can avoid this extra character being added?
Thanks in advance
The streams default to text mode, which means that in Windows, if you write \n
then the file gets \r\n
. Therefore , if you write \r\n
then the file gets \r\r\n
.
To fix this, either just write \n
in your code; or open the file in binary mode:
auto output_stream = std::ofstream(output_file.c_str(), std::ios::binary);