ostringstream and ostream
I want to do something like this:
File<<"LineA"<<endl
File<<"LineB"
and get the result in file:
LineA
LineB
but instead I get the result of:
LineALineB
File<<"LineA"<<endl
File<<"LineB"
and get the result in file:
LineA
LineB
but instead I get the result of:
LineALineB
My code is something like this:
void
LogFile::WriteLine (string Line)
{
if (this->bWriteEnable)
{
ofstream File(this->FileName.c_str(), ios::binary | ios::app);
File<<this->LineNumber<<": "<<Line<<endl;
this->LineNumber++;
File.close();
}
}
LogFile LF("log.fl");
LF.WriteLine ("LineA");
LF.WriteLine ("LineB");
However I get LineALineB instead of:
LineA
LineB
I also tried to put the endl at the begining like this:
File<<endl<<this->LineNumber<<": "<<Line;
Doesnt work either.
void
LogFile::WriteLine (string Line)
{
if (this->bWriteEnable)
{
ofstream File(this->FileName.c_str(), ios::binary | ios::app);
File<<this->LineNumber<<": "<<Line<<endl;
this->LineNumber++;
File.close();
}
}
LogFile LF("log.fl");
LF.WriteLine ("LineA");
LF.WriteLine ("LineB");
However I get LineALineB instead of:
LineA
LineB
I also tried to put the endl at the begining like this:
File<<endl<<this->LineNumber<<": "<<Line;
Doesnt work either.
Why are you opening the file in binary mode?
On MS platforms, an end-of-line is marked by \r\n.
Opening in text mode enables the translation file(\r\n)<->memory(\n), while opening in binary mode disables it.
Unless you really are manipulating binary data (which must not be translated), you have little reason to open the file with an std::ios::binary flag.
On MS platforms, an end-of-line is marked by \r\n.
Opening in text mode enables the translation file(\r\n)<->memory(\n), while opening in binary mode disables it.
Unless you really are manipulating binary data (which must not be translated), you have little reason to open the file with an std::ios::binary flag.
Ok, the problem is when I open the file with notepad it doesnt do the line downs.
However, with word pad I do see the lines one under the other.
However, with word pad I do see the lines one under the other.
Quote:Original post by The C modest god
Ok, the problem is when I open the file with notepad it doesnt do the line downs.
However, with word pad I do see the lines one under the other.
That's because wordpad is smarter than notepad, and is able to deal with UNIX end-of-line conventions (i.e. just a '\n' in the file).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement