.


:




:

































 

 

 

 


Class File




{ char filename[80]; //

fstream *fstr; //

int maxpos; //

public:

File(char *filename);

~File();

int Open(); //

const char* GetName();

int Read(inf &); //

void Remote(); //

void Add(inf); //

int Del(int pos); //

};

ostream& operator << (ostream &out, File &obj)

{ inf p;

out << "File " << obj.GetName() << endl;

obj.Remote();

while(obj.Read(p))

out<<"\n -> "<<p.cl<<' '<<p.pk<<' '<<p.sm;

return out;

}

File::File(char *_filename) //

{ strncpy(filename,_filename,80);

fstr = new fstream();

}

File::~File() //

{ fstr->close();

delete fstr;

}

int File::Open() // −

{ fstr->open(filename, ios::in | ios::out | ios::binary| ios::trunc); //

if (!fstr->is_open()) return -1;

return 0;

}

int File::Read(inf &p) // fstr p

{ if(!fstr->eof() && //

fstr->read(reinterpret_cast<char*>(&p),sizeof(inf)))

return 1; //

fstr->clear();

return 0;

}

void File::Remote() // get put

{ fstr->seekg(0,ios_base::beg); // get

fstr->seekp(0,ios_base::beg); // put

fstr->clear(); //

}

const char* File::GetName() //

{ return this->filename; }

void File::Add(inf cldata)

{ fstr->seekp(0,ios_base::end);

fstr->write(reinterpret_cast<char*>(&cldata),sizeof(inf));

fstr->flush();

}

int File::Del(int pos) // pos

{ Remote();

fstr->seekp(0,ios_base::end); // maxpos

maxpos = fstr->tellp(); // put

maxpos/=sizeof(inf);

if(maxpos<pos) return -1;

fstr->seekg(pos*sizeof(inf),ios::beg); // ,

// pos

while(pos<maxpos)

{ fstr->read(reinterpret_cast<char*>(&cldata),sizeof(inf));

fstr->seekp(-2*sizeof(inf), ios_base::cur);

fstr->write(reinterpret_cast<char*>(&cldata),sizeof(inf));

fstr->seekg(sizeof(inf),ios_base::cur);

pos++;

}

strcpy(cldata.cl,""); //

cldata.pk=0; //

cldata.sm=0;

fstr->seekp(-sizeof(inf), ios_base::end);

fstr->write(reinterpret_cast<char*>(&cldata),sizeof(inf));

fstr->flush(); //

}

int main(void)

{ int n;

File myfile("file");

if(myfile.Open() == -1)

{ cout << "Can't open the file\n";

return -1;

}

cin>>cldata.cl>>cldata.pk>>cldata.sm;

myfile.Add(cldata);

cout << myfile << endl; //

cout << " ";

cin>>n;

if(myfile.Del(n) == -1)

cout << " "<<n<<" \n";

cout << myfile << endl; //

}





:


: 2015-09-20; !; : 928 |


:

:

80% - .
==> ...

1521 - | 1367 -


© 2015-2024 lektsii.org - -

: 0.011 .