ifstrem的eof状态问题

发表于:2007-05-26来源:作者:点击数: 标签:
freebsd 5.3, gcc3.4.2 这样一段代码, ifstream infile; string line; infile.open(file1); cout infile.is_open()= infile.is_open() endl; cout infile.eof()= infile.eof() endl; while(!infile.eof()){ std::getline(infile, line); } infile.close();
freebsd 5.3, gclearcase/" target="_blank" >cc3.4.2
这样一段代码,

        ifstream infile;
        string line;

        infile.open("file1");
        cout << "infile.is_open()=" << infile.is_open() << endl;
        cout << "infile.eof()=" << infile.eof() << endl;
        while(!infile.eof()){
                std::getline(infile, line);
        }                               
        infile.close();
        infile.open("file2");
        cout << "infile.is_open()=" << infile.is_open() << endl;
        cout << "infile.eof()=" << infile.eof() << endl;
        infile.close();

输出的结果是,

infile.is_open()=1
infile.eof()=0
infile.is_open()=1
infile.eof()=1

而如果在file1的操作中去掉那段while的读操作,此时结果为,

infile.is_open()=1
infile.eof()=0
infile.is_open()=1
infile.eof()=0

看来close()并没有清除掉eof状态,解决的方法是调用一次clear()。




原文转自:http://www.ltesting.net

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)