Baca dan Tulis Objek di 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\n";
file_w.close();
ifstream file_r ("obj.txt");
file_r.read((char *)&s1, sizeof(s1));
cout<< "successfully read file\n";
char text[100];
while(file_r)
{
file_r.getline(text,100);
cout << text <<endl;
}
}
Sleep deprived