Những chủ đề không trả lời tôi:làm thế nào để tái sử dụng stringstream
How do you clear a stringstream variable?
std::ifstream file(szFIleName_p);
if(!file) return false;
// create a string stream for parsing
std::stringstream szBuffer;
std::string szLine; // current line
std::string szKeyWord; // first word on the line identifying what data it contains
while(!file.eof()){
// read line by line
std::getline(file, szLine);
// ignore empty lines
if(szLine == "") continue;
szBuffer.str("");
szBuffer.str(szLine);
szBuffer>>szKeyWord;
szKeyword
sẽ luôn chứa từ đầu tiên, szBuffer
không đang được thiết lập lại, không thể tìm thấy một ví dụ rõ ràng ở bất cứ nơi nào về cách sử dụng stringstream.
Code mới sau khi câu trả lời:
...
szBuffer.str(szLine);
szBuffer.clear();
szBuffer>>szKeyWord;
...
Ok, đó là phiên bản cuối cùng của tôi:
std::string szLine; // current line
std::string szKeyWord; // first word on the line identifying what data it contains
// read line by line
while(std::getline(file, szLine)){
// ignore empty lines
if(szLine == "") continue;
// create a string stream for parsing
std::istringstream szBuffer(szLine);
szBuffer>>szKeyWord;
bản sao có thể có của [Làm thế nào để tái sử dụng một ostringstream?] (Http://stackoverflow.com/questions/624260/how-to-reuse-an-ostringstream) – maxschlepzig