将一些数据结构写入文件 [英] Writing some data structure to file

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

问题描述

假设我有一个类 Product 的对象,它有成员变量如: price,name等
并说我有10个这样的产品对象。
现在我想写所有10个人对一些文件 Products.dat
后来我想说从文件检索第6个产品。
它是如何实现通常在C + +? (我只是感兴趣的
在检索部分)。

解决方案

我建议你开始有一个文件格式例如:

  39.28 
产品名称
其他数据
47.12
另一个产品名称
其他数据
...

文件使用 ifstream 对象,使用 getline()获取文本行一次,使用 operator>> 为一个数值(但要注意忽略其余的行,所以下一个 getline 读取空行)。跟踪您正在阅读的字段和记录,直到达到所需的记录。



如果您需要更快的访问,您需要使用固定宽度的字段,更多的输出格式的知识...最好学习作为第二步。对于固定宽度字段,您可以使用ifstream上的seek函数直接跳转到相关数据,然后使用 getline operator>> 如上所述,或者甚至 ifstream :: read()



该示例更明确 - 并添加一个product_index字段 - 按照注释。这可能需要一些调试/调整,但基本的想法有希望有用...

  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_< '\\\
'
<< p.product_index_< '\\\
'
<< p.product_name_<< '\\\
'
<< p.other_data< '\\\
';
}

friend std :: istream& operator>>(std :: ostream& is,Product& p)
{
是> p.price_>> p.product_index_;
is.ignore(std :: numeric_limits< streamsize> :: max(),'\\\
');
getline(is,p.product_name_);
getline(p.other_data);
return is;
}
};

//读取输入文件...
if(ifstream input(input.dat))
{
产品p;
while(input>> p)
do_something_with(p);

if(!input.eof())
std :: cerr<< 解析输入文件时出错\ n;
}
else
std :: cerr<< 错误打开输入文件\\\
;


Imagine I have an object of class Product, which has member variables like: price, name, etc.. And say I have 10 such Product objects. Now I want to write all 10 of them say to some file Products.dat. Later I want to say retrieve 6th product from file. How is it achieved usually in C++? (I am merely interested in retrieving part).

解决方案

I would recommend that you start by having a file format such as:

39.28
product name
other data
47.12
another product name
other data
...

You can then read through the file using an ifstream object, getting textual lines one at a time using getline(), and numeric values using operator>> to a numeric value (but be careful to ignore the rest of the line so the next getline doesn't read an empty line). Keep track of which field and record you're reading until you reach the desired record.

If you need faster access, you need to use fixed-width fields which requires a lot more knowledge of output formatting... best to learn that as a second step. With fixed-width fields you can use the seek function on ifstream to jump directly to the relevant data, then start reading using getline or operator>> as above, or even ifstream::read().

EDIT: making the example more explicit - and adding a product_index field - as per the comments. This might need a bit of debugging/tweaking, but the basic idea's hopefully useful...

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::ostream& is, Product& p)
    {
        is >> p.price_ >> p.product_index_;
        is.ignore(std::numeric_limits<streamsize>::max(), '\n');
        getline(is, p.product_name_);
        getline(p.other_data);
        return is;
    }
};

    // to read your input file...
    if (ifstream input("input.dat"))
    {
        Product p;
        while (input >> p)
            do_something_with(p);

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

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

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