C ++:在数据复杂时读/写二进制数据

时间:2013-05-02 15:11:49

标签: c++ file-io binary fstream ostream

如果我还要定义如何保存数据,我如何以二进制文件写入/读取数据? 我试图将一些简单的数据结构保存到二进制文件中。

例如,我有一个像这样的结构矢量:

struct Vertex
{
    x;
    y;
    z;
}

std::vector<Vertex> vertices;

我想将此向量保存到二进制文件中。

我知道如何使用ifstreamostream使用<<>>运算符输出它,这些运算符可以重载以处理我的数据,但它们不能输出二进制。

我也知道如何使用.write()来编写二进制文件,但问题是我无法找到重载我需要的方法来处理我的数据。

3 个答案:

答案 0 :(得分:2)

这是answer to a similar question。虽然在你的情况下这是好的,但要注意,如果你在结构中使用指针,这将无效。

指针意味着:“在这个其他内存段中加载了相关数据”,但实际上,它只包含内存的地址。然后写操作将保存此内存位置。当你将它加载回来时,内存仍然保留你想要的信息的可能性很小。

人们通常做的是创建序列化机制。在结构中添加一个方法,或者编写另一个以struct作为参数的函数,并输出一个包含您决定的特殊格式的数据的char *数组。您还需要相反的函数来从文件中读取并从二进制数据重新创建结构。你应该看看处理这个非常常见的编程问题的boost::serialization

答案 1 :(得分:1)

这样做的一种方式(不一定是最好的)是使用你选择的任何二进制写函数来编写数据,例如

write(fd,data,size);

但是将“数据”作为结构传递。

例如

Vertex data;
data.x = 0;
etc...
write(fd,(char*)&data,sizeof(data));

将结构视为字符数组,然后将它们写入文件。重读它应该与上述相反。

请注意,使用向量(动态分配并且我在内存中的奇怪位置有奇怪的东西)实际上没有一个很好的方法,所以我推荐一个结构数组。

答案 2 :(得分:0)

我一直在寻找一种高效且紧凑的方式来永久存储数据,但找不到任何可执行的最小示例,因此我为您整理了它。

这个解决方案的美妙之处在于能够随心所欲地处理“向量”中的数据并随心所欲地扩展“结构”(只需稍作更改)

通过这种方式,内存中表示的“向量”被传输到驱动器,然后通过“std::vector”提供的“data()”方法再次返回。

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#define FILENAME "data.bin"

struct Vertex
{
    size_t x{};
    size_t y{};
    std::string z{};
};

void writeVectorToBinary(std::vector<Vertex> &v);
void readVectorFromBinary(std::vector<Vertex> &v);
void printVector(std::vector<Vertex> &v);

int main()
{
std::vector<Vertex> vertices;
vertices.push_back({1,2,"three"});
vertices.push_back({4,5,"six"});

writeVectorToBinary(vertices);
printVector(vertices);
readVectorFromBinary(vertices);
printVector(vertices);

std::cin.get();
return 0;
}


void writeVectorToBinary(std::vector<Vertex> &v)
{
  size_t size = v.size();
  //Open Binary file, to write out data
  std::ofstream file(FILENAME, std::ios::binary);
  if(!file)
    std::cout << "Something went wrong" << std::endl;
  //Store/Write the vector size
  file.write(reinterpret_cast<const char*>(&size), sizeof(size));
  //Store/Write the data of the vector out
  file.write(reinterpret_cast<const char*>(v.data()), sizeof(v[0])*size);
  //Close binary file
  file.close();
}

void readVectorFromBinary(std::vector<Vertex> &v)
{
//Clear Vector just for the proof of concept
v.clear();
size_t size{};
//Open Binary file to read in data
std::ifstream file(FILENAME,std::ios::binary);
if(!file)
  std::cout << "Something went wrong" << std::endl;
//Read the vector size
file.read(reinterpret_cast<char*>(&size), sizeof(size));
//Resize vector now that its known
v.resize(size);
//Read vector data in
file.read(reinterpret_cast<char*>(v.data()), sizeof(v[0])*size);
//Close binary file
file.close();
}

void printVector(std::vector<Vertex> &v)
{
  for(size_t i{}; i < v.size(); ++i ){
    std::cout << "\n------Vector" << i << "--------" << std::endl;
    std::cout << v[i].x << std::endl;
    std::cout << v[i].y << std::endl;
    std::cout << v[i].z << std::endl;
  }
}