for some reason no matter when I put endl or \n a new line is not created in the text file. Instead it just puts a little box where the new line character is, but it doesn't actually go to a newline in the text file. This is in notepad
for some reason no matter when I put endl or \n a new line is not created in the text file. Instead it just puts a little box where the new line character is, but it doesn't actually go to a newline in the text file. This is in notepad
Does this work for you? It should be this simple...
Code:#include <fstream>
using namespace std;
int main( void )
{
ofstream fout( "MyFile.txt" );
fout << "Hello World!! \n";
fout << "This is line #2" << endl;
fout << "This is line #3";
fout.close();
return 0;
}
Yeah that works. I'm doing it differently because you cannot call ios::read and store it an unsigned char * array, but at least I know why its doing it
Code:#include <fstream.h>
int main( void )
{
ofstream fout( "MyFile.txt", ios::out | ios::binary );
fout << "Hello World!! \n";
fout << "This is line #2" << endl;
fout << "This is line #3";
fout.close();
return 0;
}
You open the file as binary, not text. That's your problem!
(newlines are for textfiles only)
>>You open the file as binary, not text.
It might work on some OSes, but on Windows a newline is actually two characters in a row, carriage return and line feed. So if you use '\n' in a binary file, it will most likely just output the carriage return.
>>So if you use '\n' in a binary file, it will most likely just output the carriage return
\n is a newline, \r is for carriage return. To do what the OP wants, use \r\n instead of just \n, but of course this isn't portable.