#include <iostream>
#include <fstream>
using namespace std;
class Writer {
public:
Writer(char* fileName):fout(fileName, ios::binary|ios::out) {}
~Writer() {
fout.close();
}
virtual Writer& operator<<(int&);
virtual Writer& operator<<(char*);
private:
ofstream fout;
};
class Reader {
public:
Reader(char *fileName):fin(fileName, ios::binary|ios::in) {}
~Reader() {
fin.close();
}
virtual Reader& operator>>(int&);
virtual Reader& operator>>(char*);
private:
ifstream fin;
};
class animal {
public:
animal(int x, char *name):age(x), myName(name) {}
animal(Reader& rdr);
int GetX() {
return age;
}
char* GetName() {
return myName;
}
virtual void write(Writer& wtr);
~animal() {}
private:
int age;
char *myName;
};
Writer& Writer::operator <<(int & arg) {
fout << arg << endl;
return *this;
}
Writer& Writer::operator <<(char * arg) {
fout << arg << endl;
return *this;
}
Reader& Reader::operator >>(int & arg) {
fin >> arg;
return *this;
}
Reader& Reader::operator >>(char * arg) {
fin >> arg;
return *this;
}
animal::animal(Reader &rdr) {
rdr>>age;
rdr>>myName;
}
void animal::write(Writer &wtr) {
wtr<<age;
wtr<<myName;
}
const int Max = 5;
int main(int argc, char* argv[]) {
char filename[16];
char buffer[255];
int pointAge;
cout<<"vvedite im`a faila"<<endl;
cin.getline(filename, 16);
animal *Bob;
Writer *w = new Writer(filename);
for(int i=0; i<Max; i++) {
cout<<"zapis ob`ekta"<<endl;
cin.getline(buffer, 255);
cout<<"vvedite vozrast jivotnogo"<<endl;
cin>>pointAge;
cin.ignore(1, '\n');
Bob = new animal(pointAge, buffer);
Bob->write(*w);
delete Bob;
}
delete w;
Reader *r = new Reader(filename);
for(int i=0; i<Max; i++) {
Bob = new animal(*r);
cout<<"nomer__"<<i<<endl;
cout<<Bob->GetName()<<"__"<<Bob->GetX()<<endl;
delete Bob;
}
delete r;
char q;
cout << "\nEnter anything for exit..";
cin >> q;
return 0;
}
у меня пример программы которая записывает данные в файл, но делает это не корректно, помогите пожалуйста начинающему разобраться
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
class Writer
{
public:
Writer(char* fileName):fout(fileName, ios::binary){}
~Writer(){fout.close();}
virtual Writer& operator<<(int&);
virtual Writer& operator<<(char*);
private:
ofstream fout;
};
class Reader
{
public:
Reader(char *fileName):fin(fileName, ios::binary){}
~Reader(){fin.close();}
virtual Reader& operator>>(int&);
virtual Reader& operator>>(char*&);
private:
ifstream fin;
};
class Storable
{
public:
Storable(){}
Storable(Reader&){}
virtual void write(Writer&)=0;
};
class animal: public Storable
{
public:
animal(int x, char *name):age(x), myName(name){}
animal(Reader& rdr);
int GetX(){return age;}
char* GetName(){return myName;}
virtual void write(Writer& wtr);
~animal(){}
private:
int age;
char *myName;
};
Writer& Writer::operator <<(int & arg)
{
fout.write((char*)&arg, sizeof(int));
return *this;
}
Writer& Writer::operator <<(char * arg)
{
fout.write(arg, sizeof(arg));
return *this;
}
Reader& Reader::operator >>(int & arg)
{
fin.read((char*)&arg, sizeof(int));
return *this;
}
Reader& Reader::operator >>(char *&arg)
{
fin.read(arg, sizeof(arg));
return *this;
}
animal::animal(Reader &rdr)
{
rdr>>age;
rdr>>myName;
}
void animal::write(Writer &wtr)
{
wtr<<age;
wtr<<myName;
}
const int Max = 5;
int _tmain(int argc, _TCHAR* argv[])
{
char filename[16];
char buffer[255];
int pointAge;
cout<<"vvedite im`a faila"<<endl;
cin.getline(filename, 16);
animal *Bob;
Writer *write = new Writer(filename);
for(int i=0; i<Max; i++)
{
cout<<"zapis ob`ekta"<<endl;
cin.getline(buffer, 255);
cout<<"vvedite vozrast jivotnogo"<<endl;
cin>>pointAge;
cin.ignore(1, '\n');
Bob = new animal(pointAge, buffer);
Bob->write(*write);
delete Bob;
}
Reader * read = new Reader(filename);
for(int i=0; i<Max; i++)
{
Bob = new animal(*read);
cout<<"nomer__"<<i<<endl;
cout<<Bob->GetName()<<"__"<<Bob->GetX()<<endl;
delete Bob;
}
system("pause");
return 0;
}
Сделал чтобы работало, а что там намутили с классами -- даже разбираться не стал.
Спасибо!