Need a project done?

C++ Programming Developer

Search This Blog

Write Objct to File C++

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

/////////////////////////////////////////////////////////////////////////////////////////////////
class person{
protected:
char name[80];
short age;
public:
void getData(){
cout << "Enter name: "; cin.getline(name,80,'\n');
cout << "Enter age: "; cin >> age;
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////

int main(){
person pers;
pers.getData();

//open file.
ofstream os("PERSON.DAT", ios::binary);
//write this object to file.
os.write(reinterpret_cast<char*>(&pers), sizeof(person));
cout << "FILE WRITTEN" << endl;
return 0;
}

Now

Read Object from File C++

No comments:

Post a Comment

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