I don't think he has programmed in C++ before
but soon we had hacked the below program together
It writes an object to a file
but we can not seem to read it in as an object?
any help welcome
The idea is to eventually marry this with other work
on pbase which we started some time ago
Code: Select all
#include <fstream>
#include <iostream>
using namespace std;
class person {
protected: string name, fname; short age;
public: string getFileName() {
fname = name;
fname.append(".dat");
return fname;
}
public: string getName() {
return name;
}
public: void getData(){
cout << "Enter name: "; cin >> name;
cout << "Enter age: "; cin >> age;
}
};
int main() {
//person newPerson;
//newPerson.getData();
//char filename[80];
//strcpy(filename, newPerson.getFileName().c_str());
//ofstream outfile(filename, ios::binary);
//outfile.write(reinterpret_cast<char*>(&newPerson), sizeof(newPerson));
char filename[80];
cout << "Enter filename to read: "; cin >> filename;
person oldPerson;
ifstream infile(filename, ios::binary);
infile.read(reinterpret_cast<char*>(&oldPerson), sizeof(oldPerson));
//cout << oldPerson.getName();
return 0;
}