从/从结构/类读取/写入文件 [英] Reading/writing files to/from a struct/class

查看:137
本文介绍了从/从结构/类读取/写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读一个文件到一个结构或类,但在一些阅读后,我收集了它不是一个好主意做类似的:

I'd like to read a file into a struct or class, but after some reading i've gathered that its not a good idea to do something like:

int MyClass::loadFile( const char *filePath ) {

            ifstream file ( filePath, ios::in | ios::binary );

            file.read ((char*)this, 18);

            file.close();

            return 0;

        }



我想知道如果我想写一个文件一个结构/类这不是犹太教:

I'm guessing if i want to write a file from a struct/class this isn't kosher either:

void MyClass::writeFile( string fileName ) {

        ofstream file( fileName, ofstream::binary ); 

        file.write((char*)this, 18);

        file.close();

    }

这听起来像我不想做的原因这是因为即使我的结构的数据成员加起来18个字节,其中一些可能在内存中用额外的字节填充。有没有更正确/优雅的方式来获得一个类/结构像这样的文件?

It sounds like the reason i don't want to do this is because even if the data members of my struct add up to 18 bytes, some of them may be padded with extra bytes in memory. Is there a more correct/elegant way to get a file into a class/struct like this?

推荐答案

称为序​​列化。

它比二进制表示更脆弱。但它有需要解释的开销。标准类型适用于序列化,并且鼓励您将类序列化,以便包含您的类的类可以轻松地序列化。

It is less brittle than a binary representation. But it has the overhead of needing to be interpreted. The standard types work well with serialization and you are encouraged to make your class serialize so that a class containing your class can easily be serialized.

class MyClass {
     int x;
     float y;
     double z;
     friend std::ostream& operator<<(std::ostream& s, MyClass const& data);
     friend std::istream& operator>>(std::istream& s, MyClass& data);
};

std::ostream& operator<<(std::ostream& s, MyClass const& data)
{
    // Something like this
    // Be careful with strings (the input>> and output << are not symmetric unlike other types)
    return str << data.x << " " << data.y << " " << data.z << " ";
}

// The read should be able to read the version printed using <<
std::istream& operator>>(std::istream& s, MyClass& data)
{
    // Something like this
    // Be careful with strings.
    return str >> data.x >> data.y >> data.z;
}

用法:

int main()
{
    MyClass   plop;
    std::cout << plop;  // write to a file
    std::cin  >> plop;  // read from a file.


    std::vector<MyClass>  data;

    // Read a file with multiple objects into a vector.
    std::ifstream  loadFrom("plop");
    std::copy(std::istream_iterator<MyClass>(loadFrom), std::istream_iterator<MyClass>(),
              std::back_inserter(data)
             );


    // Write a vector of objects to a file.
    std::ofstream   saveTo("Plip");
    std::copy(data.begin(), data.end(), std::ostream_iterator<MyClass>(saveTo));

    // Note: The stream iterators (std::istream_iterator) and (std::ostream_iterator)
    //       are templatized on your type. They use the stream operators (operator>>)
    //       and (operator<<) to read from the stream.
}

这篇关于从/从结构/类读取/写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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