2013-12-18 61 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); 
}