Ebi wrote:
> It's a main function of club program in Borland c++ 5;
> There is a film class in my club program...
> But I have a problem with it: whenever I add a film by addfilm function
> to film.dat file, data(objects) that I add before remove, and there is
> an object available at per time. And I can't add more than one film,
> because the previous film object remove.
> Please help me.
>
> //********************************************** 1.addfilm **
> void addfilm()
> {
> film film1;
> film1.setstate(1);
> ofstream fp("film.dat", ios::binary);
Here is your problem. You are opening the file in the default ios:trunc
mode, which destroys the content of the file you just opened. try using
ofstream fp("film.dat", ios::binary | ios::app);
Any output you make to fp will be appended to the end of the file
(unless you use seekp()...)
> if(!fp) {
> cout<<"Cannot open file."<<endl;
> system("PAUSE");
> exit(0); }
> cout<<endl;
> film1.puttitle();
> film1.setid();
> cout<<"Enter Artists: ";
> cin>>film1.artists;
> cout<<"Enter Director: ";
> cin>>film1.director;
> cout<<"Enter Company: ";
> cin>>film1.company;
> cout<<"Enter Producer: ";
> cin>>film1.producer;
> cout<<endl;
>
> fp.seekp(sizeof(class film)*film1.getid(), ios::beg);
> fp.write((char*)&film1, sizeof(class film));
> fp.close(); }
>
--
Det är bara töntar som har en .sig
( - Remove capital X from email to reply - )


|