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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12372531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:12:42  来源:igfitidea点击:

Reading and writing a std::vector into a file correctly

c++file-iostliostreamstdvector

提问by FacundoGFlores

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?

那么,如何进行操作呢?

回答by Platinum Azure

Try using an ostream_iterator/ostreambuf_iterator, istream_iterator/istreambuf_iterator, and the STL copymethods:

尝试使用ostream_iterator/ ostreambuf_iteratoristream_iterator/istreambuf_iterator和 STLcopy方法:

#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));

回答by ForEveR

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]));

回答by piokuc

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`

回答by Igor R.

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 vectorobject internal implementation; vector element size is sizeof(std::vector<your_type>::value_type).

此外,sizeof(vector<your_type>)只是vector对象内部实现的大小;向量元素大小为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); 

回答by Jan

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). // write myVector.size() to file, then

// 假设 outf 是一个可写的文件指针(二进制)。// 将 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.

一个弱点是这种方式不支持字节顺序。