将 C++ 向量读取和写入文件 [英] Reading and writing C++ vector to a file

查看:14
本文介绍了将 C++ 向量读取和写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些图形工作,我需要尽快读取大量数据,并且理想情况下希望直接将数据结构读取和写入磁盘.基本上,我加载了各种文件格式的 3d 模型,加载时间过长,因此我想将它们以准备好的"格式写出作为缓存,以便在程序的后续运行中加载得更快.

For some graphics work I need to read in a large amount of data as quickly as possible and would ideally like to directly read and write the data structures to disk. Basically I have a load of 3d models in various file formats which take too long to load so I want to write them out in their "prepared" format as a cache that will load much faster on subsequent runs of the program.

这样做安全吗?我担心的是直接读入向量的数据?我已经删除了错误检查,硬编码 4 作为 int 的大小等等,以便我可以给出一个简短的工作示例,我知道这是错误的代码,我的问题实际上是在 C++ 中读取整个数组是否安全结构直接变成这样的向量?我相信确实如此,但是当您开始低级并像这样直接处理原始内存时,C++ 有很多陷阱和未定义的行为.

Is it safe to do it like this? My worries are around directly reading into the data of the vector? I've removed error checking, hard coded 4 as the size of the int and so on so that i can give a short working example, I know it's bad code, my question really is if it is safe in c++ to read a whole array of structures directly into a vector like this? I believe it to be so, but c++ has so many traps and undefined behavour when you start going low level and dealing directly with raw memory like this.

我意识到数字格式和大小可能会因平台和编译器而异,但这甚至只能由同一个编译器程序读取和写入,以缓存以后运行同一程序时可能需要的数据.

I realise that number formats and sizes may change across platforms and compilers but this will only even be read and written by the same compiler program to cache data that may be needed on a later run of the same program.

#include <fstream>
#include <vector>

using namespace std;

struct Vertex
{
    float x, y, z;
};

typedef vector<Vertex> VertexList;

int main()
{
    // Create a list for testing
    VertexList list;
    Vertex v1 = {1.0f, 2.0f,   3.0f}; list.push_back(v1);
    Vertex v2 = {2.0f, 100.0f, 3.0f}; list.push_back(v2);
    Vertex v3 = {3.0f, 200.0f, 3.0f}; list.push_back(v3);
    Vertex v4 = {4.0f, 300.0f, 3.0f}; list.push_back(v4);

    // Write out a list to a disk file
    ofstream os ("data.dat", ios::binary);

    int size1 = list.size();
    os.write((const char*)&size1, 4);
    os.write((const char*)&list[0], size1 * sizeof(Vertex));
    os.close();


    // Read it back in
    VertexList list2;

    ifstream is("data.dat", ios::binary);
    int size2;
    is.read((char*)&size2, 4);
    list2.resize(size2);

     // Is it safe to read a whole array of structures directly into the vector?
    is.read((char*)&list2[0], size2 * sizeof(Vertex));

}

推荐答案

正如 Laurynas 所说,std::vector 保证是连续的,所以它应该可以工作,但它可能是不可移植的.

As Laurynas says, std::vector is guaranteed to be contiguous, so that should work, but it is potentially non-portable.

在大多数系统上,sizeof(Vertex) 将为 12,但填充结构体的情况并不少见,因此 sizeof(Vertex) == 16.如果您在一个系统上写入数据,然后在另一个系统上读取该文件,则无法保证它会正常工作.

On most systems, sizeof(Vertex) will be 12, but it's not uncommon for the struct to be padded, so that sizeof(Vertex) == 16. If you were to write the data on one system and then read that file in on another, there's no guarantee that it will work correctly.

这篇关于将 C++ 向量读取和写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆