2013-12-18 60 views
-1

目前我只能用一行讀取文件,但我需要重新寫入幾行文本,並將每行放入單獨的字符串中。按行讀取輸入行

ifstream file("test.txt"); 
ostringstream ss; 
cout << "File" << file.get << endl; 
ss << file.rdbuf(); 
cout << ss.str() << endl; 
const string& s = ss.str(); 

這段代碼只是讀取一行並將其粘貼到字符串s中。

+1

不應該'file.get'是'file.get()'?請發佈**實際**代碼。 – 2013-12-18 22:02:08

+0

該代碼應該工作。 – 0x499602D2

回答

1
std::ifstream stream("file.txt"); 
std::string line; 
std::vector<std::string> lines; 
while (std::getline(stream, line)) { 
    lines.push_back(line); 
}