Search code examples
c++fileeol

How to change EOL coding in txt files?


I am writing in Visual Studio 2008 in C++ and I have problems with other libraries - they do not accept the line endings (EOL) I generate with my txt files.

How can I change that while writing a file with

std::ofstream myFile;
myFile.open("traindata.txt");
myFile << "stuff" << endl;
// or 
//myFile << "stuff" << '\n';
myFile.close();

EDIT 2 :

Ok, I did a mistake in code : I was appending "0 " for every iteration so that I had whitespace before the EOL.

By bad. You guys have been right. Thanks for help.


Solution

  • Is it possible that you just don't want the \n to end of line sequence to happen? Open your file using std::ios_base::binary: this turns off precisely the conversion. ... and don't use std::endl unless you really want to flush the stream:

    std::ofstream myFile("traindata.txt", std::ios_base::binary);
    myFile << "stuff\n";
    

    The close() is typically also unnecessary unless you want to check that it was successful.