小编Joh*_*ann的帖子

clang 3.3/Xcode&libc ++:std :: getline在调用ifstream :: clear()后不读取数据

以下程序演示了libc ++和libstdc ++之间std :: getline行为的不一致性(使用clang3.3).

程序打开文件testfile,读取它直到eof,然后使用ifstream :: clear清除错误位并再次尝试从同一个文件句柄读取以查看是否有新数据附加到文件中.

#include <fstream>
#include <iostream>
#include <unistd.h>

using namespace std;

int main() {
    ifstream* file = new ifstream("testfile");
    if ( ! file->is_open() ) {
        cout << "testfile does not exist" << endl;
        return -1;
    }

    while ( 1 ) {
        file->clear(); // remove end of file evil bits
        string line;

        // workaround:
        // file->seekg(file->tellg());

        while ( getline(*file, line) )
            cout << "read line: " << line << endl;

        if ( file->eof() )
            cout …
Run Code Online (Sandbox Code Playgroud)

c++ clang ifstream libstdc++ libc++

5
推荐指数
1
解决办法
510
查看次数

标签 统计

c++ ×1

clang ×1

ifstream ×1

libc++ ×1

libstdc++ ×1