Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

read and write objects in cpp

#include <iostream>
#include <fstream>
using namespace std;

class student
{
public:
    char name[20], id[10], level[10];

    void getdata()
    {
        cout <<" Enter your name: ";
        cin >> name;

        cout <<" Enter your id: ";
        cin >> id;

        cout <<" Enter your level: ";
        cin >> level;
    }
};

int main()
{
    student s1,s2;

    s1.getdata();
    cout<< endl;
    s2.getdata();

    ofstream file_w ("obj.txt");
    file_w.write((char *)&s1, sizeof(s1));
    file_w.write((char *)&s2, sizeof(s2));

    cout<< "successfully written in file
";
    file_w.close();

    ifstream file_r ("obj.txt");
    file_r.read((char *)&s1, sizeof(s1));

    cout<< "successfully read file
";

    char text[100];
    while(file_r)

    {
        file_r.getline(text,100);
        cout << text <<endl;
    }
}
 
PREVIOUS NEXT
Tagged: #read #write #objects #cpp
ADD COMMENT
Topic
Name
5+7 =