5

考虑以下示例代码:

#include <iostream>

using namespace std;

int main()
{
  istreambuf_iterator<char> eos;
  istreambuf_iterator<char> iit(cin.rdbuf());
  int i;
  for (i = 0; iit != eos; ++i, ++iit) {
    cout << *iit;
  }
  cout << endl << i << endl;
}

以及一个包含以下内容的输入文件:“foo\xffbar”:

$ hexdump testin
0000000 66 6f 6f ff 62 61 72
0000007

现在使用 clang libc++ 与 gnu libstdc++ 进行测试:

$ make test
clang++ -std=c++11 -stdlib=libc++ -Wall -stdlib=libc++ -o bug-libcc bug.cpp
clang++ -std=c++11 -stdlib=libc++ -Wall -stdlib=libstdc++ -o bug-libstd bug.cpp
./bug-libcc < testin
foo
3
./bug-libstd < testin
foo�bar
7

如您所见,libc++ 版本认为 0xff 是流的结尾并停止读取。所以这导致了几个问题。

1) 这是我应该报告的 libc++ 中的错误吗?我对现有错误的谷歌搜索一无所获。

2)有没有解决这个问题的好方法?

编辑

以下代码有效:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  ifstream ifs ("testin", ios::binary);
  istreambuf_iterator<char> eos;
  istreambuf_iterator<char> iit(ifs.rdbuf());
  int i;
  for (i = 0; iit != eos; ++i, ++iit) {
    cout << *iit;
  }
  cout << endl << i << endl;
}

让我相信这是一个二进制转换问题,但这并不能解释为什么 libstdc++ 可以正常工作。

编辑2

使用没有二进制文件的文件也可以正常工作:

ifstream ifs ("testin");

所以肯定有一些可疑的事情发生。看起来这可能是 cin 实现中的问题,而不是迭代器。

4

3 回答 3

5

不幸的是,libc++ 中仍然存在一个错误(除了 ecatmur 指出的那个)。这是修复:

Index: include/__std_stream
===================================================================
--- include/__std_stream    (revision 176092)
+++ include/__std_stream    (working copy)
@@ -150,7 +150,7 @@
     {
         for (int __i = __nread; __i > 0;)
         {
-            if (ungetc(__extbuf[--__i], __file_) == EOF)
+            if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF)
                 return traits_type::eof();
         }
     }

我会尽快检查。对不起这个错误。感谢您引起我的注意。

修复已提交的修订版 176822 到 libcxx 公共 svn 主干。即使修复在标头中,修复也需要重新编译的 dylib。

于 2013-03-11T19:45:46.943 回答
2

我想你可能发现了一个已经修复的错误。 此提交(由@Howard Hinnant 提供)包含以下更改:

@@ -104,7 +104,7 @@
     int __nread = _VSTD::max(1, __encoding_);
     for (int __i = 0; __i < __nread; ++__i)
     {
-        char __c = getc(__file_);
+        int __c = getc(__file_);
         if (__c == EOF)
             return traits_type::eof();
         __extbuf[__i] = static_cast<char>(__c);
@@ -131,7 +131,7 @@
                 if (__nread == sizeof(__extbuf))
                     return traits_type::eof();
                 {
-                    char __c = getc(__file_);
+                    int __c = getc(__file_);
                     if (__c == EOF)
                         return traits_type::eof();
                     __extbuf[__nread] = static_cast<char>(__c);

您会注意到旧版本存储了getcinto的返回值char,这是一个禁忌,因为它会将char0xffint值混淆EOF(即-1)。

该错误仅适用于,cin因为受影响的方法是 on __stdinbuf,这是 libc++ 仅用于实现的类型cinifstream例如使用basic_filebuf<char>.

检查libcxx/include/__std_stream系统上的文件,看看它是否有这个错误;如果是这样,应用补丁,它应该修复它。

于 2013-03-11T19:12:21.217 回答
1

迭代器正在从流中提取。
需要使用binary模式打开流以防止对原始数据进行任何转换。

接下来,不要使用char. 类型可以是有char符号、无符号或无符号,具体取决于编译器。我建议uint8_t在读取二进制八位字节时使用。

尝试这样的事情:

#include <cstdint>
using std::uint8_t;
istreambuf_iterator<uint8_t> eos;
于 2013-03-11T17:31:20.373 回答