编程论坛
注册
登录
编程论坛
→
C++教室
从文件中如何读取一行到String类的对象中?
fzujj
发布于 2008-01-11 17:55, 5957 次点击
在写一个书目索引程序,但不知道怎么从文件中一行一行的读到String类中,直到文件结束.
3 回复
#2
中学者
2008-01-11 18:18
ifstream in;
string buffer;
in.open(" 文件");
if(in)
getline(in,buffer);
esle cerr<<"wrong!"<<endl;
in.close();
#3
zxj1102
2008-01-11 22:38
vector<string> srccode;
string line;
ifstream infile(filename);
if(!infile) // if not exists
{ throw runtime_error("Can't read file :");}
while (!infile.eof())
{
getline(infile, line);
srccode.push_back(line);
}
infile.close();
#4
fzujj
2008-01-12 21:54
呵呵,昨晚看了2楼描述getline函数的用法,我自己该进了,也是用容器来存储,和3楼的写法差不多.那个书目索引程序写完了,谢谢2楼的 也谢谢3楼的!
1