将endline插入到字符串流中

Emi*_*ier 6 c++ newline stringstream end-of-line

我们知道,当插入\n文件流时,系统的相应行尾序列将被写入文件(例如,\r\n用于Windows).是否在std::stringstream结果中插入一个结束于系统适当的行尾序列被写入字符串?例如:

#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???
}
Run Code Online (Sandbox Code Playgroud)

Bjö*_*lex 15

系统特定的行结尾仅与文本文件相关.只要流只在内存中,它就是'\n'.

  • 除了字符串流中的单个字符'\n'之外,它怎么可能呢?假设翻译是在字符串流上完成的.想想`sstream1 <<"\n"会导致的混乱; sstream2 << stream1.str(); ...; std :: cout << sstream99.str();` (6认同)