Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting endline into a stringstream

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???
}
like image 450
Emile Cormier Avatar asked Jul 28 '11 20:07

Emile Cormier


People also ask

Does Stringstream ignore newline?

Stringstream don't copy new lines.

Can you pass a Stringstream in C++?

Absolutely! Make sure that you pass it by reference, not by value.

What is the difference between Istringstream and Stringstream?

A stringstream is an iostream object that uses a std::string as a backing store. An ostringstream writes to a std::string . An istringstream reads from a std::string . You read & write from & to an istringstream or ostringstream using << and >> , just like any other iostream object.

How does Stringstream work in C++?

The stringstream class in C++ allows a string object to be treated as a stream. It is used to operate on strings. By treating the strings as streams we can perform extraction and insertion operation from/to string just like cin and cout streams.


1 Answers

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

like image 177
Björn Pollex Avatar answered Sep 21 '22 12:09

Björn Pollex