繁体   English   中英

读写矢量 <bool> 到C ++中的文件

[英]Reading and writing vector<bool> to a file in c++

我有一个向量,其大小可能真的很大(一百万个元素)。 我将向量的内容作为字节值写入到文件中。 我无法弄清楚如何将字节值读回到向量中。

这是代码:

#include <fstream>
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;

int main()
{
  // Filling a vector with values
  std::vector<bool> ve;
  ve.push_back(true);
  ve.push_back(false);
  ve.push_back(true);
  ve.push_back(false);
  ve.push_back(true);
  // Printing the values of the vector
  for(unsigned int i = 0; i < ve.size(); i++)
      cout << ve.at(i) << ".";
  cout << endl;

  // Writing the vector contents to a file
  const char* file_name = "abc.txt";
  ofstream outfile(file_name, ios::out | ios::binary);
  outfile.write((const char*)&(ve[0]), ve.size());
  outfile.close();
  // Reading the file and filling the vector with values
  ifstream infile ("abc.txt", ifstream::binary);
  vector<bool> out_ve((std::istreambuf_iterator<char>(infile)),
                       std::istreambuf_iterator<char>());

  while( !infile.eof() )
      out_ve.push_back(infile.get());

  // Checking if the values read are the same as the original values
  cout << "SIZE: " << out_ve.size() << endl;
  for(unsigned int i = 0; i < out_ve.size(); i++)
    cout << out_ve.at(i) << ".";
  cout << endl;

  infile.close();
  return 0;
}

[edit]写入后关闭文件,输出与输入非常不同。

1.0.1.0.1.
SIZE: 6
1.1.1.0.1.1.

如何将正确的元素放入向量out_ve?

从大多数STL容器写入数据无法通过outfile.write((const char*)&(ve[0]), ve.size()); 因为他们以复杂的方式管理内存,这对于他们的运作方式至关重要。 使用vector ,它可以工作,因为内存存储是连续的,但是vector<bool>是特殊的,因为它将多个vector<bool>打包到一个字节中。 正如评论者已经指出的那样, ve[0]返回一个特殊的临时准引用类型,并且通过强制转换为char*写出该引用将产生与向量中的数据完全无关的东西。

即使此构造使您可以访问向量的原始存储器,但用于写出数据的代码与用于读入数据的代码也不兼容。 您用于写出数据的代码会将 8个bool条目打包到每个char ,但是您用于读取数据的代码会将每个char转换为单个bool

由于您正在使用istreambuf_iterator回读数据,所以为什么不以相同的方式将其写出:

std::copy(ve.begin(), ve.end(), std::ostreambuf_iterator<char>(outfile));

这样每字节写出一个bool

如果您想以打包表示形式写出数据,每个bool写一个位,我认为您需要发明自己的输入和输出迭代器。

vector<bool>不是真实的vector 您在其他地方找到的可用于向量的代码不会。 并且您一定不能忽略“临时地址”。 这条线

outfile.write((const char*)&(ve[0]), ve.size());

不适用于vector<bool>

问题在于您要处理的地址不是您认为的类型。

尝试执行AND操作:

#define logical_and(x, y)   ((x==y) ? x | y)

您应该研究运算符重载,并使运算符返回文件中的下一个无符号字符,然后使用atoi将值转换为整数

防爆。

template<typename T>
  T operator << (ifstream& file)
  {
    return reinterpret_cast<T>(file.get());
  };

以上仅是一个示例(未经测试。一段时间未使用c ++模板,因此可能需要重新制作)

祝你好运

亚历山大·弗兰克兰(Tandex)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM