By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Filing problem!

P: n/a
Ebi
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(); }

Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a
Ebi
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.

Jul 23 '05 #3

P: n/a
"Ebi" <kh*****@gmail.com> 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
Jul 23 '05 #4

P: n/a
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
kb******@gascad.at
Jul 23 '05 #5

P: n/a
Ebi
> 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?

Jul 23 '05 #6

P: n/a
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
kb******@gascad.at
Jul 23 '05 #7

P: n/a
Ebi
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!

Jul 23 '05 #8

P: n/a
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::out | ios::in | ios::binary );

if( !OutFile ) {
// File does not exist yet, create one
OutFile.clear();
OutFile.open( "C:\\test.dat", ios::out | 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::out | 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
kb******@gascad.at
Jul 23 '05 #9

P: n/a
Ebi
Thanks a million for yor help!
my problem solve very nicely!

film film1;
fstream fp("film.dat", ios::out | ios::in | ios::binary);
if(!fp) {
fp.clear();
fp.open( "film.dat", ios::out | 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

Jul 23 '05 #10

P: n/a

"Ebi" <kh*****@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Thanks a million for yor help!
my problem solve very nicely!

film film1;
fstream fp("film.dat", ios::out | ios::in | ios::binary);
if(!fp) {
fp.clear();
fp.open( "film.dat", ios::out | 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


If I understand correctly, if you don't use ios::in, then it's assumed to be
a *new* file that you're simply going to write to. So when you open it, it
starts at zero size (empty).

-Howard
Jul 23 '05 #11

P: n/a
Ebi
if I don't use ios::in, then it's assumed to be a *new* file that I am
simply going to write to. and previous objects lost. too last object
always stay in file, not all of them!
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 )?
--
Ebrahim Khademi

Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.