Reading a class object from ASCII file...
Code:
#include <iostream> // cin, cout
#include <fstream> // ifstream, ofstream
#include <cstring> // strcpy, strlen, strncmp
#include <cctype> // toupper
#include <cstdlib> // itoa
using namespace std;
class Person
{
private:
int nr;
char* Name;
public:
Person()
{
Name = "Testdrive";
nr = 123456;
}
Person(int i)
{
Name = "ABCDEF";
nr = i;
}
int getnr()
{
return nr;
}
char* getname()
{
return Name;
}
};
//GLOBAL VARIABLES
Person* p = new Person();
Person* p2 = new Person(124124);
//GLOBAL FUNCTIONS
void ReadFile()
{
int i;
char* c;
cout << endl << endl << " READING FILE " << endl;
ifstream innfil("person.dta");
while (innfil.eof() > 0) { If I change this one innfil.eof, something bad is happening.
innfil >> i >> c; //IF I let it be, it just skips the code within this while-loop.
cout << endl << " from read file " << i
<< " name from read " <<c << endl;
}
}
void WriteFile()
{
ofstream out("person.dta");
cout << p->getnr() << p->getname();
out << p->getnr()<< " " << p->getname();
cout << p2->getnr() << p2->getname();
//out << p2->getnr()<< " " << p2->getname();
out.close();
}
//MAIN PROGRAMME
int main()
{
WriteFile();
ReadFile();
cout << "done";
char c;
cin >> c;
}
Edited code:
Edit: Wondering how to solve this, seriously... Can someone show me an example, fix mine? :)