Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Filing problem!

Reply
Thread Tools

Filing problem!

 
 
Ebi
Guest
Posts: n/a
 
      02-03-2005
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);
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(); }

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-03-2005
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);
> if(!fp) {
> cout<<"Cannot open file."<<endl;
> system("PAUSE");
> exit(0); }
> cout<<endl;
> film1.puttitle();
> film1.setid();


'setid' to what?

> 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);


This is dependent on 'getid()'. If 'getid()' always returns 0, you
will always rewind to the beginning and write over the very first one
saved.

> fp.write((char*)&film1, sizeof(class film));
> fp.close(); }
>


V
 
Reply With Quote
 
 
 
 
Ebi
Guest
Posts: n/a
 
      02-03-2005
void setid()
{
int a;
cout<<"Difine id of film:";
cin>>a;
id=a;
}
int getid() {return id;}

these functions in film class, get the id of film and save the object
at
fp.seekp(sizeof(class film)*film1.getid(), ios::beg);
fp.write((char*)&film1, sizeof(class film));

I enter 1, 2 and 3 for id every time, and so getid() should return
these value, not 0 !
what do you think about it?
Help me.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-03-2005
"Ebi" <> wrote...
> void setid()
> {
> int a;
> cout<<"Difine id of film:";
> cin>>a;
> id=a;
> }
> int getid() {return id;}
>
> these functions in film class, get the id of film and save the object
> at
> fp.seekp(sizeof(class film)*film1.getid(), ios::beg);
> fp.write((char*)&film1, sizeof(class film));
>
> I enter 1, 2 and 3 for id every time, and so getid() should return
> these value, not 0 !


Should. Does it? What's the definition of your 'film' class?

> what do you think about it?


I think you need to debug your program.

> Help me.


Ask particular questions.

V


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      02-03-2005
Ebi wrote:
>
> void setid()
> {
> int a;
> cout<<"Difine id of film:";
> cin>>a;
> id=a;
> }
> int getid() {return id;}
>
> these functions in film class, get the id of film and save the object
> at
> fp.seekp(sizeof(class film)*film1.getid(), ios::beg);
> fp.write((char*)&film1, sizeof(class film));
>
> I enter 1, 2 and 3 for id every time, and so getid() should return
> these value, not 0 !
> what do you think about it?


Don't assume anything when you debug your program.
Check it!

It isn't that hard to write:

size_t Index = film1.getid();
cout << "Writing with id " << Index << endl;
fp.seekp(sizeof(class film)*Index, ios::beg);

and then you know for sure.

Seriously: When debugging, quesionize each and everything. Eg. the
above:

void setid()
{
int a;
cout<<"Difine id of film:";
cin>>a;
id=a;
}

A good idea is to immediatly output what the stream has read,
just to verify that everything is ok.

void setid()
{
int a;
cout<<"Difine id of film:";
cin>>a;
id=a;

cout << "New id equals " << id << endl;
}

You are not the first one claiming that a variable has a specific value,
while in reality this is not true.

--
Karl Heinz Buchegger

 
Reply With Quote
 
Ebi
Guest
Posts: n/a
 
      02-03-2005
> I enter 1, 2 and 3 for id every time, and so getid() shoul*d return
> these value, not 0 !


>Should. Does it? What's the definition of >your 'film' clas*s?

//***********film class*****************
class film {
private:
int state; // state of field record
int status; //0: in
//customer id: out
char title[21];
int id;



public:
film();
void puttitle() {
char n[21];
cout<<"Difine title of film:";
cin.get(n,20);
strcpy(title,"");
strcpy(title,n);
}
char *gettitle(){return title;}

int getstate() {return state;}
void setstate(int b) {state=b;}

int getstatus() {return status;}
void setstatus(int b) {status=b;}

int getid() {return id;}
void setid()
{
int a;
cout<<"Difine id of film:";
cin>>a;
id=a;
}

void print() {
cout<<endl;
if (status==0)cout<<title<<" is avalible";
else cout<<"is not avalible & customer id: "<<status;
cout<<"\ntitle: "<<title;
cout<<"\nfilm id: "<<id;
cout<<"\nartiststs: "<<artists;
cout<<"\nproducer: "<<producer;
cout<<"\ndirector: "<<director;
cout<<"\ncompany: "<<company;
cout<<endl;
}

char artists[100];
char producer[21];
char director[21];
char company[21];
};
>I think you need to debug your program.

How? my program is a collection of 2 classes for film & customer, and
other functions that add and removed and... film object on a file, so
what should I debug? there isn't any object in my program except
temperory objects in some functions that receive data and save on
file.
How I can debug it?

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      02-03-2005
Ebi wrote:
>
> >I think you need to debug your program.

> How? my program is a collection of 2 classes for film & customer, and
> other functions that add and removed and... film object on a file, so
> what should I debug? there isn't any object in my program except
> temperory objects in some functions that receive data and save on
> file.
> How I can debug it?


By running your program in a debugger and checking all the variables.
If you don't have a debugger, insert output statements into the
program to get a look at the variables.

--
Karl Heinz Buchegger

 
Reply With Quote
 
Ebi
Guest
Posts: n/a
 
      02-03-2005
Mr.Bazarov & Mr.Heinz Buchegger
Thanks a million for your help!,
One of my friends find problem very nicely:

instead of use ofstream fp("film.dat", ios::binary)
use : ofstream fp("film.dat", ios::binary | ios::app); OR
ofstream fp("film.dat", ios::binary | ios::ate);
OR
combine those two commands;
maybe solve you problem.
--
Sincerely Yours
Ebrahim Khademi

but ios::app dosn't work correctly in my random access file saving,
because objects save in the end of file, and I can't save them in:
fp.seekp(sizeof(class film)*film1.getid(), ios::beg);

too ios::ate and combine of them dosn't solve the problem.
please Help me with other ios::func!

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      02-03-2005
Ebi wrote:
>
> too ios::ate and combine of them dosn't solve the problem.
> please Help me with other ios::func!
>


This works for me as expected.


#include <iostream>
#include <fstream>

using namespace std;

struct Test
{
int Id;
char Name[20];
};

int main()
{
ofstream OutFile;

OutFile.open( "C:\\test.dat", ios:ut | ios::in | ios::binary );

if( !OutFile ) {
// File does not exist yet, create one
OutFile.clear();
OutFile.open( "C:\\test.dat", ios:ut | ios::binary );
}

Test Rec;

Rec.Id = 1;
strcpy( Rec.Name, "Eibi" );
OutFile.seekp( 1 * sizeof( Rec ), ios::beg );
OutFile.write( (char*)&Rec, sizeof( Rec ) );

OutFile.close();
OutFile.open( "C:\\test.dat", ios:ut | ios::in | ios::binary );

Rec.Id = 0;
strcpy( Rec.Name, "Kalle" );
OutFile.seekp( 0 * sizeof( Rec ), ios::beg );
OutFile.write( (char*)&Rec, sizeof( Rec ) );

OutFile.close();



ifstream InFile( "C:\\test.dat", ios::binary );

InFile.seekg( 0 * sizeof( Rec ), ios::beg );
InFile.read( (char*)&Rec, sizeof( Rec ) );
cout << Rec.Id << " " << Rec.Name << endl;

InFile.seekg( 1 * sizeof( Rec ), ios::beg );
InFile.read( (char*)&Rec, sizeof( Rec ) );
cout << Rec.Id << " " << Rec.Name << endl;
}


--
Karl Heinz Buchegger

 
Reply With Quote
 
Ebi
Guest
Posts: n/a
 
      02-03-2005
Thanks a million for yor help!
my problem solve very nicely!

film film1;
fstream fp("film.dat", ios:ut | ios::in | ios::binary);
if(!fp) {
fp.clear();
fp.open( "film.dat", ios:ut | ios::binary );
}
...
...
...
...

fp.seekp(sizeof(class film)*film1.getid(), ios::beg);
fp.write((char*)&film1, sizeof(class film));
fp.close();

fstream without ios::in was cause of removing previous data from the
file! what you think about it, I used fstream only for write data! so
we shouldn't need ios::in? and what is the relationship beetween two (
lack of ios::in & removing previous data from the file )?

I love programming and you!
--
Sincerely Yours
Ebrahim Khademi

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question About AOL Filing Cabinet: Forwarding email from AOL Filing Cabinet Cliff Computer Support 2 11-13-2006 08:14 AM
Filing problem! Ebi C++ 0 02-03-2005 01:44 AM
Bookmark Filing Conundrum mapmaker Firefox 2 12-21-2004 03:22 AM
Indexing & Filing Software Suggestion Requested (that will run under W98) ? Robert11 Digital Photography 2 05-21-2004 09:17 PM
filing photos Neil Digital Photography 1 08-24-2003 11:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57