使用fstream将类对象写入文件,然后读取它 [英] Writing a class object into a file using fstream and then read it

查看:333
本文介绍了使用fstream将类对象写入文件,然后读取它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个学生的班,并获取3个输入信息,并输出此文件。如何做到这一点?这是我的尝试:

  #include< iostream& 
using namespace std;
class Student {
private:
char name [50];
char id [50];
int age
public:
void getdata()
{
//以name为输入
//以id作为输入
//以age为输入
}
void showdata()
{
//显示存储的文件
}
}

int main $ b {
Student s1;
ofstream s1(student.txt); //我想存储's1'对象
//其他
return 0;
}


解决方案

C ++使用流范式实现标准输入/输出。



流范式意味着如果应用程序想访问/使用资源(文件,控制台等),流将充当应用程序和资源之间的中介:

  ofstream + ---- + 
+ ------------- -------> | File |
| + ---- +
|
+ ------ + ------ +
|应用|
+ ------ + ------ +
|
| + ------- +
+ --------------------> | Console |
cout + ------- +

这意味着每个写/你执行的是真正的流操作。这点的一点是,流操作基本上是一样的,与你使用的资源类型(和什么类型的流)无关。



通用(对任何类型的流/资源有效的通用含义)。怎么样? 重载C ++运算符>>和<



对于输入操作(输入意味着从流接收数据, / object),我们需要重载>>运算符,如下所示:

  istream& operator>>>(istream& is,MyClass& object)
{
是> object.myClassAtributte; (1)
... //对于你的类的每个属性都相同。

return is;
}

首先,注意输入流是通过引用传递的。通过引用,因为流是不可复制的(什么意思是复制流?复制你的应用程序和资源之间的链接?这听起来很可笑),和非const beacuse你要修改流(你要写通过它)。



最后,注意函数不返回void,返回一个引用传递给函数的同一个流。这允许你编写连接写/读的句子,例如 cout<< Hello。<< Im< Manu343726< endl;



对于输出操作(输出意味着发送数据到流),我们需要重载<运算符,其实现方式完全相同:

  ostream& operator<<<(ostream& os,const MyClass& object)
{
os< object.myClassAtributte; (1)
... //对于你的类的每个属性都相同。

return os;
}

注意,在这种情况下,你的对象通过const,beacuse,

最好实现这个函数,使他们成为你的类的朋友,以允许我们访问私人/受保护的成员。


I want to make a class of a student and take 3 inputs information and make an output of this file. How to this? This is my try:

#include <iostream>
using namespace std;
class Student{
  private:
    char name[50];
    char id[50];
    int age;
  public:
    void getdata()
    {
        //take name as input
        //take id as input
        //take age as input
    }
    void showdata()
    {
         //display stored file
    }
 }

int main()
{
    Student s1;
    ofstream s1("student.txt");      //i want to store that 's1' object
    //anything else
    return 0;
}

解决方案

C++ uses the stream paradigm to implement standard input/output.

Stream paradigm means that if you application wants to access/use a resource (A file, the console, etc) a stream acts as a mediator between your application and the resource:

                     ofstream       +----+
              +-------------------->|File|
              |                     +----+
              |
       +------+------+
       | Application |
       +------+------+
              |
              |                     +-------+
              +-------------------->|Console|
                        cout        +-------+

It means every write/read operations you perform are really stream operations. The point of this is that stream operations are basically the same, independently of what type of resource (And what type of stream) are you using.

This allows us to implement a "generic" (Generic meaning valid for any type of stream/resource). How? Overloading C++ operators >> and <<.

For input operations (Input means receiving data from the stream and put it in our variable/object), we need to overload the >> operator as follows:

istream& operator>>(istream& is , MyClass& object)
{
    is >> object.myClassAtributte; (1)
    ... //Same for every attribute of your class.

    return is;
}

First, note that the input stream is passed by reference. By reference because streams are non-copyable (What exactly means to copy a stream? Copy the link between your app and the resource? That sounds ridiculous), and non-const beacuse you are going to modify the stream (You are going to write through it).

Finally, note that the function not returns void, returns a reference to the same stream that was passed to the function. That allows you to write concatenated-write/read sentences like cout << "Hello. " << "Im" << " Manu343726" << endl;

For output operations (Output means sending data to the stream), we need to overload the << operator, wich implementation is exactly the same:

ostream& operator<<(ostream& os , const MyClass& object)
{
    os << object.myClassAtributte; (1)
    ... //Same for every attribute of your class.

    return os;
}

Note that in this case, your object is passed const, beacuse we won't modify it (We will only read its attributes).

(1) Is preferable to implement this functions making them friend of your class, to allow us access to private/protected members.

这篇关于使用fstream将类对象写入文件,然后读取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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