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???
}
Stringstream don't copy new lines.
Absolutely! Make sure that you pass it by reference, not by value.
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.
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.
The system specific line endings are only relevant for text files. As long as the stream is only in memory, it is just '\n'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With