当我将字符串传递到字符串流时,特殊字符会消失。我试过这个可以直接测试的代码:
#include <iostream>
#include <sstream>
using namespace std;
int main(int argc, char* argv[]) {
string txt("hehehaha\n\t hehe\n\n<New>\n\ttest:\t130\n\ttest_end:\n<New_end>\n");
cout << txt << endl; // No problem with new lines and tabs
stringstream stream;
stream << txt;
string s;
while(stream >> s) {
cout << s; // Here special characters like '\n' and '\t' don't exist anymore.
}
cout << "\n\n";
return 0;
}
我能做些什么来克服这个问题?
编辑:我试过这个:
stream << txt.c_str();
它奏效了。但我不知道为什么...