数组存储问题 [英] Array storing problem

查看:82
本文介绍了数组存储问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友.
我在C ++中制作了100个整数元素的数组.当我在数组中输入100条记录时,它将存储这些记录,但是当我关闭程序时,我的所有100条记录都被销毁了.
我想将这些数组元素存储在文件中,以便当我重新打开程序时可以在数组中再次使用它们.
所以,Plz告诉我

1.如何将这些数组元素存储在文件中.
2.重新打开程序时,如何再次使用这些数组元素.

Dear Friends.
I have made an array of 100 integer elements in C++. When I enters the 100 records in the array, it stores these records but when I closed the program all of my 100 records are destroyed.
I want to store these array elements in a file so that when I re-open my program I can use them again in my array.
So, Plz tell me that

1. How can I store these array elements in a file.
2. How can I use these array elements again when I re-open my program.

推荐答案

编写一个实现数组的类,您可以从一个类中派生您的类STL或MFC上的预制数组类.
在类析构函数中,将数组保存到文件中;在类构造函数中,尝试从文件中加载数组.如果加载数组失败,则使用默认值初始化该数组.
Write a class that implements your array, you could derive your class from one of the pre-made array classes on STL or MFC.
In the class destructor save the array to file and in the class constructor try to load the array from file; if loading the array fails initialize the array with default values.


查找std :: fstream,这是在磁盘外获取数据的标准方法.您可以使用与读取和写入控制台相同的代码从磁盘加载和保存数据.

与Sauro不同,我不会从标准库容器类中派生任何东西-它们并不是真的为它设计的.相反,我会看一下标准的库复制功能(std :: copy)以及它是如何工作的. std :: ostream_iterator和std :: istream_iterator是使一切正常工作所需的粘合剂.

干杯,

Look up std::fstream, it''s the standard way of getting data on an off disk. You can use much the same code to load and save data from disk as you would for reading and writing to the console.

Unlike Sauro I wouldn''t derive anything from a standard library container class - they''re not really designed for it. Instead I''d look at the standard library copy function (std::copy) and how that works. std::ostream_iterator and std::istream_iterator are the glue you''ll need to get everything working together.

Cheers,

Ash


这篇关于数组存储问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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