473,503 Members | 5,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Filing problem!

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
11 2154
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
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
"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
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
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
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
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

117
7102
by: Peter Olcott | last post by:
www.halting-problem.com
0
298
by: Ebi | last post by:
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,...
2
1930
by: j. shepherd | last post by:
I am looking for a filing software for the office. I need to get all of the files in the office entered into some type of software or database, that I can pull up scanned documents, location of...
5
6072
by: jwright | last post by:
I have decided to use a struct to collect my data. The input file is comma dilineated between almost all of the fields. Here is the code I have so far and a sample input and output file. ...
1
7583
by: editprod | last post by:
I'd like to create an Access database for my the central filing system of my office. My boss can never find where files are. We have approx. 2000 files in a dozen file drawers, each drawer with a...
1
2071
by: RANIA | last post by:
i wana make a program in which user find the index of agiven string through filing and also user can delete the given string from file replace and isert a string into fil.
9
1607
by: uppili4chi | last post by:
Good morning my dear friends, I am uppili from India. i am working in one mnc. I need a help very urgent. "My problem i need to write a program for search a file just like search option at...
0
1387
by: reddog | last post by:
Can anyone direct me to a site (or help me) that will tell me how to set up Direct Digitial Filing? Iam running Windows XP. Thank You for Your Valued Time Reddog
0
1059
by: Chicago | last post by:
$9.95 Online Tax Filing www.MHBSgloabl.com
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.