문자열을 문자열로 전달하면 특수 문자가 사라집니다. 내가 이것을 극복하기 위해 할 수있는 일Stringstream은 새 줄을 복사하지 않습니다.
#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();
그것은 일 : 나는 이것을 시도했다. 하지만
당신은 >> 임의의 공백에서 분할을 알고 있습니까? – PlasmaHH
나는 그렇지 않았습니다. 그러나 stringstream이 아니라 ifstream을 사용할 때는 ifstream >> char을 사용합니다. 공백은 무시되지 않고 char로 전달됩니다. stringstream을 사용하는 이유는 무엇입니까? –
@ AlexandreToqué >> char는 >> 문자열과 동일하지 않습니다. – doctorlove