Need a project done?

C++ Programming Developer

Search This Blog

Read Object from File C++

After an object has been written in a file, you can read that object from file in C++.
I've written an object at Write Objct to File C++.

After writing this object, following is the program to read an object from a file.

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

/////////////////////////////////////////////////////////////////////////////////////////////////
class person{
protected:
char name[80];
short age;
public:
void showData(){
cout << "Name: " << name << endl;
cout << "Age: " <<  age << endl;
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////

int main(){
person pers;

//open file.
ifstream is("PERSON.DAT", ios::binary);
//read an object from this file.
is.read(reinterpret_cast<char*>(&pers), sizeof(person));
//after reading object from file, Show it.
pers.showData();
return 0;
}

No comments:

Post a Comment

"Don't let anyone ever make you feel like you don't deserve what you want."