#include <stdlib.h>
#include <fstream.h>
class Test
{
public:
Test(int weight, long days):itsWeight(weight), itsNumbersDaysAlive(days){}
~Test(){}
int GetWeight() const { return itsWeight; }
void SetWeight(int weight) { itsWeight = weight; }
long GetDaysAlive() const { return itsNumbersDaysAlive; }
void SetDaysAlive(long days) { itsNumbersDaysAlive = days; }
private:
int itsWeight;
long itsNumbersDaysAlive;
};
int main()
{
char fileName[80];
cout << "Please enter the file name: ";
cin >> fileName;
ofstream fout(fileName, ios::binary);
if (!fout)
{
cout << "Unable to open " << fileName << " for writing.\n";
return(1);
}
Test T1(50, 100);
fout.write((char *) &T1, sizeof T1);
fout.close();
ifstream fin(fileName, ios::binary);
if (!fin)
{
cout << "Unable to open " << fileName << " fo reading.\n";
return(1);
}
Test T2(1, 1);
cout << "T2 weight: " << T2.GetWeight() << endl;
cout << "T2 days: " << T2.GetDaysAlive() << endl;
fin.read((char *) &T2, sizeof T2);
cout << "T2 weight: " << T2.GetWeight() << endl;
cout << "T2 days: " << T2.GetDaysAlive() << endl;
fin.close();
system("pause");
return 0;
}
U cemu je problem?