【问题标题】:Load binary file using fstream使用 fstream 加载二进制文件
【发布时间】:2009-07-20 18:01:15
【问题描述】:

我正在尝试通过以下方式使用fstream 加载二进制文件:

#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>

using namespace std;

int main()
{
    basic_fstream<uint32_t> file( "somefile.dat", ios::in|ios::binary );

    vector<uint32_t> buffer;
    buffer.assign( istream_iterator<uint32_t, uint32_t>( file ), istream_iterator<uint32_t, uint32_t>() );

    cout << buffer.size() << endl;

    return 0;
}

但它不起作用。在 Ubuntu 中,它因std::bad_cast 异常而崩溃。在 MSVC++ 2008 中,它只打印 0。

我知道我可以使用file.read 来加载文件,但我想使用迭代器和operator&gt;&gt; 来加载部分文件。那可能吗? 为什么上面的代码不起作用?

【问题讨论】:

  • 上述二进制数据的来源是什么?
  • 二进制数据可以是图像或类似的东西。

标签: c++ stl file-io filestream fstream


【解决方案1】:
  1. istream_iterator 想要 basic_istream 作为参数。
  2. 不可能在basic_istream 类中重载operator&gt;&gt;
  3. 定义全局operator&gt;&gt;会导致与类成员operator&gt;&gt;的编译时冲突。
  4. 您可以将basic_istream 专门用于uint32_t 类型。但是对于专业化,您应该重写 basic_istream 类的所有功能。相反,您可以定义虚拟类 x 并为其专门化 basic_istream,如下代码所示:
using namespace std;

struct x {};
namespace std {
template<class traits>
class basic_istream<x, traits> : public basic_ifstream<uint32_t>
{
public:
    explicit basic_istream<x, traits>(const wchar_t* _Filename, 
        ios_base::openmode _Mode, 
        int _Prot = (int)ios_base::_Openprot) : basic_ifstream<uint32_t>( _Filename, _Mode, _Prot ) {}

    basic_istream<x, traits>& operator>>(uint32_t& data)
    {
        read(&data, 1);
        return *this;
    }
};
} // namespace std 

int main() 
{
    basic_istream<x> file( "somefile.dat", ios::in|ios::binary );
    vector<uint32_t> buffer;
    buffer.assign( istream_iterator<uint32_t, x>( file ), istream_iterator<uint32_t, x>() );
    cout << buffer.size() << endl;
    return 0;
}

【讨论】:

  • 太棒了,这正是我所需要的。
  • 请注意,在专注于 uint32_t 时,您实际上是在专注于基本类型,unsigned int 或(不太可能)unsigned longtypedef 不会生成由模板区分的类型(或者实际上是由 C++ 中的任何其他类型的类型重载)。
【解决方案2】:

您可以重新加载运算符>> 以正确读取整数。当然,它所做的只是 read() 4 个字节。但这就是所有其他运营商>>最终都会做的事情。

这里是示例(没有错误检查,假设字节序与当前编译器使用的相同,等等)

std::istream& operator>>(std::istream& in, uint32_t& data)
{
    in.read(&data, sizeof(data));
    return in;
}

为您自己的整数风格量身定制(可能必须一次读取一个字节并移位分配它们,如果您不知道字节顺序,请在十六进制编辑器中查看文件),添加错误检查,您应该能够使用您现有的代码。

编辑:啊,是的,确保这个 shadows 提供了读取整数的 stl 运算符——可能必须从你正在使用的流中派生你自己的类并使用它而不是 std::istream& in,这样编译器就知道了首先检查谁。

【讨论】:

  • istream_iterator 使用basic_istream 作为参数,所以我不能只从basic_ifstream 继承并覆盖operator&gt;&gt;
【解决方案3】:

主要问题可能是您所说的“二进制文件”是什么意思。 ios::binary 仅确保 istream 对象不会用 '\n' 替换特定于平台的换行符。没有其他的。这对你来说足够了吗?

istream_iterator 基本上只是调用operator&gt;&gt; 的一种奇特方式。如果您的流中有真正的二进制数据,那将失败。您的文件中有真正的二进制数据吗?还是整数存储为字符串?

如果您需要读取真正的二进制整数,您需要的是istream.read() 或直接使用流缓冲区对象。

【讨论】:

  • 我有真正的二进制数据。不是文字。主要问题是“为什么它不起作用”。我已经指出 istream_iterator 我有 uint32_t 数据,而不是 char(第二个模板参数)。
  • 是的,重载 >> 读取 sizeof(uint32_t) 字节。根据谁以及如何写入文件,您可能还需要修复字节序。
  • @Eugene,你会发布一些样本吗?我认为运算符重载是不够的。
  • "如果您需要读取真正的二进制整数,您需要的是 istream.read() 或直接使用流缓冲区对象。"
  • 已发布示例。真的没有自动的方法来关注那些评论线程,是吗?
【解决方案4】:

与 Alexey Malistov 的回答相同的另一种方法:

#include <fstream>
#include <iterator>
#include <vector>
#include <iostream>

struct rint // this class will allow us to read binary
{
  // ctors & assignment op allows implicit construction from uint
  rint () {}
  rint (unsigned int v) : val(v) {}
  rint (rint const& r) : val(r.val) {}
  rint& operator= (rint const& r) { this->val = r.val; return *this; }
  rint& operator= (unsigned int r) { this->val = r; return *this; }

  unsigned int val;

  // implicit conversion to uint from rint
  operator unsigned int& ()
  {
    return this->val;
  }
  operator unsigned int const& () const
  {
    return this->val;
  }
};

// reads a uints worth of chars into an rint
std::istream& operator>> (std::istream& is, rint& li)
{
  is.read(reinterpret_cast<char*>(&li.val), 4);
  return is;
}

// writes a uints worth of chars out of an rint
std::ostream& operator<< (std::ostream& os, rint const& li)
{
  os.write(reinterpret_cast<const char*>(&li.val), 4);
  return os;
}

int main (int argc, char *argv[])
{
  std::vector<int> V;

  // make sure the file is opened binary & the istream-iterator is
  // instantiated with rint; then use the usual copy semantics
  std::ifstream file(argv[1], std::ios::binary | std::ios::in);
  std::istream_iterator<rint> iter(file), end;
  std::copy(iter, end, std::back_inserter(V));

  for (int i = 0; i < V.size(); ++i)
    std::cout << std::hex << "0x" << V[i] << std::endl;

  // this will reverse the binary file at the uint level (on x86 with
  // g++ this is 32-bits at a time)
  std::ofstream of(argv[2], std::ios::binary | std::ios::out);
  std::ostream_iterator<rint> oter(of);
  std::copy(V.rbegin(), V.rend(), oter);

  return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 2021-01-13
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    相关资源
    最近更新 更多