473,656 Members | 2,824 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.arti sts;
cout<<"Enter Director: ";
cin>>film1.dire ctor;
cout<<"Enter Company: ";
cin>>film1.comp any;
cout<<"Enter Producer: ";
cin>>film1.prod ucer;
cout<<endl;

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

Jul 23 '05 #1
11 2178
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.arti sts;
cout<<"Enter Director: ";
cin>>film1.dire ctor;
cout<<"Enter Company: ";
cin>>film1.comp any;
cout<<"Enter Producer: ";
cin>>film1.prod ucer;
cout<<endl;

fp.seekp(sizeof (class film)*film1.get id(), 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.get id(), 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.get id(), 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.get id(), 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(){ret urn 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<<"\nartist sts: "<<artists;
cout<<"\nproduc er: "<<producer ;
cout<<"\ndirect or: "<<director ;
cout<<"\ncompan y: "<<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.get id(), 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.d at", ios::out | ios::in | ios::binary );

if( !OutFile ) {
// File does not exist yet, create one
OutFile.clear() ;
OutFile.open( "C:\\test.d at", 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.d at", 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.d at", 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.get id(), 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

117
7175
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, 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...
2
1943
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 file folder in the filing cabinet, contents, etc. Because we are a small town government I also would need it to have retention dates. I don’t know what product is the best for the job. does anyone know of anything out there that would do the...
5
6087
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. #include <fstream> #include <iostream> #include <iomanip> using namespace std; struct pyrll
1
7597
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 master category (company, goverment, organiations, archives, etc.) and containng folders sorted alphabetically by subcategories or topics. There's a lot of overlap and imprecise naming for the subcategories. The database should provde the means to...
1
2080
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
1617
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 start menu in our computer". plse give idea, if it possible code.. its very urgent...........
0
1401
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
1069
by: Chicago | last post by:
$9.95 Online Tax Filing www.MHBSgloabl.com
0
8382
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8498
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7311
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.