Inserting endline into a stringstream

Emile Cormier picture Emile Cormier · Jul 28, 2011 · Viewed 17.8k times · Source

We know that when inserting \n in a file stream, the appropriate end-of-line sequence for the system will be written to the file (e.g. \r\n for Windows). Does inserting an endline in a std::stringstream result in the system-appropriate end-of-line sequence being written to the string? For example:

#include <sstream>

int main()
{
    std::ostringstream oss;
    oss << std::endl;
    std::string endlineSequence = oss.str();
    bool isWindows = enlineSequence == "\r\n";
    bool isOldMac  = endlineSequence == "\r";
    bool isUnix    = endlineSequence == "\n";
    // Will this work???
}

Answer

Bj&#246;rn Pollex picture Björn Pollex · Jul 28, 2011

The system specific line endings are only relevant for text files. As long as the stream is only in memory, it is just '\n'.