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; }
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
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)
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
>>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.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
>>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.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]