简体   繁体   English

正确地读取和写入std :: vector到文件中

[英]Reading and writing a std::vector into a file correctly

That is the point. 这就是重点。 How to write and read binary files with std::vector inside them? 如何在其中写入和读取带有std :: vector的二进制文件?

I was thinking something like: 我想的是:

//============ WRITING A VECTOR INTO A FILE ================
const int DIM = 6;
int array[DIM] = {1,2,3,4,5,6};
std::vector<int> myVector(array, array + DIM);
ofstream FILE(Path, ios::out | ofstream::binary);
FILE.write(reinterpret_cast<const char *>(&myVector), sizeof(vector) * 6);
//===========================================================

But I don't know how to read this vector. 但我不知道如何阅读这个载体。 Because I thought that the following was correctly but it isn't: 因为我认为以下是正确的,但它不是:

ifstream FILE(Path, ios::in | ifstream::binary);
FILE.read(reinterpret_cast<const char *>(&myVector), sizeof(vector) * 6);

So, how to perform the operation? 那么,如何进行操作?

Try using an ostream_iterator / ostreambuf_iterator , istream_iterator / istreambuf_iterator , and the STL copy methods: 尝试使用ostream_iterator / ostreambuf_iteratoristream_iterator / istreambuf_iterator和STL copy方法:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

#include <fstream> // looks like we need this too (edit by π)

std::string path("/some/path/here");

const int DIM = 6;
int array[DIM] = {1,2,3,4,5,6};
std::vector<int> myVector(array, array + DIM);
std::vector<int> newVector;

std::ofstream FILE(path, std::ios::out | std::ofstream::binary);
std::copy(myVector.begin(), myVector.end(), std::ostreambuf_iterator<char>(FILE));

std::ifstream INFILE(path, std::ios::in | std::ifstream::binary);
std::istreambuf_iterator iter(INFILE);
std::copy(iter.begin(), iter.end(), std::back_inserter(newVector));

Use boost::serialization . 使用boost::serialization

If you don't want use boost - write size and vector . 如果你不想使用boost - 写入大小和vector

size_t sz = myVector.size();
FILE.write(reinterpret_cast<const char*>(&sz), sizeof(sz));
FILE.write(reinterpret_cast<const char*>(&myVector[0]), sz * sizeof(myVector[0]));

You can use 您可以使用

#include <boost/serialization/vector.hpp>

to serialize your vector. 序列化你的矢量。 Read a tutorial here: http://www.boost.org/libs/serialization/doc/tutorial.html#stl ` 在这里阅读教程: http// www.boost.org/libs/ serialization/ doc/tutorial.html#stl`

Before reading vector , you should resize it: yourVector.size(numberOfElementsYouRead) . 在读取vector之前,您应该调整它的大小: yourVector.size(numberOfElementsYouRead)

Besides, sizeof(vector<your_type>) is just the size of the vector object internal implementation; 此外, sizeof(vector<your_type>)只是vector对象内部实现的大小; vector element size is sizeof(std::vector<your_type>::value_type) . 向量元素大小是sizeof(std::vector<your_type>::value_type)

Then read it like this: 然后像这样读:

file.read(reinterpret_cast<char *>(&myVector[0]), sizeof(vector<int>::element_type) * element_count); 

I used the fact that the data() method returns an address you can use for reading AND for writing. 我使用了data()方法返回一个可用于读取和写入的地址的事实。

// Assume outf is a writable filepointer (binary). //假设outf是一个可写的文件指针(二进制)。 // write myVector.size() to file, then //然后将myVector.size()写入文件

fwrite(myVector.data(), sizeof(decltype(myVector)::value_type), myVector.size(), outf);

to read: 读书:

// read MyVector.size() from file as nv, inpf is read filepointer //从文件中读取MyVector.size()作为nv,inpf是读取文件指针

MyVector.resize(nv);
fread(MyVector.data(), sizeof(decltype(MyVector)::value_type), nv, inpf);

clinging to old ways in file io, but please ignore that (even if it might irritate you :)). 坚持文件io中的旧方法,但请忽略它(即使它可能会刺激你:))。

A weakness is that endianness is unsupported in this way. 缺点是以这种方式不支持字节序。

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

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