getline的误用


ifstream stream("log.conf");

char buffer[1024];

while(stream.good())
{
stream.getline(buffer,sizeof(buffer));
//some process about buffer

这段代码害我不浅:当被读文件的一行大于1024字节时,stream不再good,导致循环退出,后面的行再也不会读到了!
正确的应该这样,以避免有行长度的限制:

string line;

while(getline(stream,line))
{
//some process about line

STL库居然有两个getline,一个是fstream的成员函数,另一个是全局函数(没有行长度限制),岂不气刹人也么哥!


相关文章

分类

留言:

关于文章

This page contains a single entry by DongHao published on 01 26, 2007 4:49 PM.

应届毕业生 was the previous entry in this blog.

相忘于江湖 is the next entry in this blog.

Find recent content on the main index or look in the 存档 to find all content.