使用流C ++进行序列化 [英] Doing serialization using streams C++

查看:138
本文介绍了使用流C ++进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个论坛上的一些帮助之后使用C ++实现了一些序列化/反序列化。
文件似乎被正确写入,但是当我阅读它时,新行被忽略,并且一些数据沿着彼此打印,例如:

I implemented some serialization / deserialization using C++ after some help on this forum. The file seems to be written correctly but when I read it, new lines are ignored and some data is printed along each other, like this:

这是我的代码。任何反馈,帮助,如何改进纠正它,非常感谢:

This is my code. Any feedback, help, how to improve correct it, greatly appreciated:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>      // std::ifstream
using namespace std;

struct Product
{
    double price_;
    double product_index_;
    std::string product_name_;
    std::string other_data_;

    friend std::ostream& operator<<(std::ostream& os, const Product& p)
    {
        return os << p.price_ << '\n'
                  << p.product_index_ << '\n'
                  << p.product_name_ << '\n'
                  << p.other_data_ << '\n';
    }

    friend std::istream& operator>>(std::istream& is, Product& p)
    {
        is >> p.price_ >> p.product_index_;
        is.ignore(std::numeric_limits<streamsize>::max(), '\n');

        getline(is,p.product_name_);
        getline(is,p.other_data_);

        return is;
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    Product s1,s2,s3,s4;

    s1.price_ = 100;
    s1.product_index_ = 0;
    s1.product_name_= "flex";
    s1.other_data_ = "dat001";

    s2.price_ = 200;
    s2.product_index_ = 1;
    s2.product_name_= "brr";
    s2.other_data_ = "dat002";

    s3.price_ = 300;
    s3.product_index_ = 2;
    s3.product_name_= "megatex";
    s3.other_data_ = "dat003";

    // write
    fstream file1("c:\\test.dat",ios::out|ios::binary|ios::app);
    file1_file << s1 << s2 << s3;
    file1_file.close();

    // read
    ifstream file2("c:\\test.dat");

    Product p;
    while (file2 >> p)
    {
            cout<<p.price_<<endl;
            cout<<p.product_index_<<endl;
            cout<<p.product_name_;
            cout<<p.other_data_;
    }

    if (!file2.good())
         std::cerr << "error during parsing of input file\n";
    else
        std::cerr << "error opening input file\n";

    return 0;
}

此外,为什么最终会出错?

Also, why do I get the error in the end?

PS。与使用读写的方法相比,上述方法的好处是: write(record,sizeof(Product)) seek * n,SEEK_SET) read(record,sizeof(Product))(读取第n个产品)提示:我听说过POD的相关限制和可移植性。

PS. What is the benefit of above approach as compared to approach using read and writes, like: write(record, sizeof(Product)) and seek(record_size * n, SEEK_SET), read(record, sizeof(Product)) (reading n-th product); hint: I've heard POD related limitations and portability

推荐答案

今天你几乎不需要实现自己的低级序列化。

Today you almost never need to implement your own low-level serialization.

尝试 JSON BSON 协议缓冲区 MessagePack XML

相当多的 libraries ,它会比roll-your-own好。

There are quite a lot of libraries, that will do it better than "roll-your-own"...

你打算写一个二进制序列化,您插入EOL('\\\
')字符。这不是将二进制流划分为令牌的好选择。

You intended to write a binary serialization, but you insert EOL ('\n') characters. That's not a good choice of dividing a binary stream into tokens.

这篇关于使用流C ++进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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