clang 3.3 / Xcode& libc ++:std :: getline在调用ifstream :: clear()后不读取数据。 [英] clang 3.3/Xcode & libc++: std::getline does not read data after calling ifstream::clear()

查看:217
本文介绍了clang 3.3 / Xcode& libc ++:std :: getline在调用ifstream :: clear()后不读取数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



程序打开文件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;

//解决方法:
// file-> seekg(file-> tellg());

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

if(file-> eof())
cout<< getline \\\
后的文件报告eof;

usleep(1000000);
}
}



使用libstdc ++(无论哪个编译器)在程序运行时将数据附加到testfile,数据将在下一次循环迭代中由getline调用读取。



在使用libc ++的OS-X上的clang 3.3中,遇到文件结束后,getline将总是失败,并在所有后续调用中设置eof位,读取已附加到文件的任何数据。取消注释解决方法,只是寻求到当前位置恢复libstdc ++行为。使用clang ++ -stdlib = libstdc ++编译libstdc ++也会恢复到旧的行为。



有人知道这是否是预期的变化?我试图这样做的方式是不支持或者是当前libc ++版本的这个问题?



触发这种行为对我来说,clang版本是最多的最近一个发布XCode为小牛:

  Apple LLVM版本5.0(clang-500.2.79)(基于LLVM 3.3svn) 
目标:x86_64-apple-darwin13.0.0
线程模型:posix

链接的libc ++库具有此版本信息:

  / usr / lib / libc ++ 1.dylib:
/usr/lib/libc++.1.dylib(兼容性版本1.0.0,当前版本120.0.0)
/usr/lib/libc++abi.dylib(兼容性版本1.0.0,当前版本48.0。 0)
/usr/lib/libSystem.B.dylib(兼容性版本1.0.0,当前版本1197.1.1)

$ b $要将 ifstream 的内部缓冲区与外部设备同步,您需要调用:



  file-> sync(); 


The following program demonstrates an inconsistency in std::getline behavior between libc++ and libstdc++ (using clang3.3).

The program opens the file testfile, reads it until eof, then clears the error bits using ifstream::clear and tries to read from the same filehandle again to see if new data was appended to the file.

#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 << "File reports eof after getline\n";

        usleep(1000000);
    }
}

Using libstdc++ (no matter which compiler), if you append data to testfile while the program is running, the data will be read by the getline call in the next loop iteration.

In clang 3.3 on OS-X with libc++, after the first time the file end is encountered, getline will always fail and set the eof bit on all following calls, without reading any data that has been appended to the file. Uncommenting the workaround, which just seeks to the current position restores libstdc++ behavior. Compiling against libstdc++ with clang++ -stdlib=libstdc++ also reverts to the old behavior.

Does anyone know if this is an expected change? Am I trying to do this in a way that is not supported or is this some problem of current libc++ versions?

The clang version that triggers this behavior for me is the most recent one shipped with XCode for Mavericks:

Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix

The libc++ library that is linked against has this version information:

/usr/lib/libc++.1.dylib:
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)
/usr/lib/libc++abi.dylib (compatibility version 1.0.0, current version 48.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)

解决方案

To synchronize the internal buffer of the ifstream with the external device you need to call:

file->sync();

这篇关于clang 3.3 / Xcode&amp; libc ++:std :: getline在调用ifstream :: clear()后不读取数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆