Filing problem!

Discussion in 'C++' started by Ebi, Feb 3, 2005.

  1. Ebi

    Ebi Guest

    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(); }
     
    Ebi, Feb 3, 2005
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Feb 3, 2005
    #2
    1. Advertising

  3. Ebi

    Ebi Guest

    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.
     
    Ebi, Feb 3, 2005
    #3
  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 !


    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
     
    Victor Bazarov, Feb 3, 2005
    #4
  5. 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
     
    Karl Heinz Buchegger, Feb 3, 2005
    #5
  6. Ebi

    Ebi Guest

    > 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?
     
    Ebi, Feb 3, 2005
    #6
  7. 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
     
    Karl Heinz Buchegger, Feb 3, 2005
    #7
  8. Ebi

    Ebi Guest

    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!
     
    Ebi, Feb 3, 2005
    #8
  9. 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::eek:ut | ios::in | ios::binary );

    if( !OutFile ) {
    // File does not exist yet, create one
    OutFile.clear();
    OutFile.open( "C:\\test.dat", ios::eek: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::eek: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
     
    Karl Heinz Buchegger, Feb 3, 2005
    #9
  10. Ebi

    Ebi Guest

    Thanks a million for yor help!
    my problem solve very nicely!

    film film1;
    fstream fp("film.dat", ios::eek:ut | ios::in | ios::binary);
    if(!fp) {
    fp.clear();
    fp.open( "film.dat", ios::eek: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
     
    Ebi, Feb 3, 2005
    #10
  11. Ebi

    Howard Guest

    "Ebi" <> wrote in message
    news:...
    > Thanks a million for yor help!
    > my problem solve very nicely!
    >
    > film film1;
    > fstream fp("film.dat", ios::eek:ut | ios::in | ios::binary);
    > if(!fp) {
    > fp.clear();
    > fp.open( "film.dat", ios::eek: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
    >


    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
     
    Howard, Feb 3, 2005
    #11
  12. Ebi

    Ebi Guest

    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
     
    Ebi, Feb 4, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ebi

    Filing problem!

    Ebi, Feb 3, 2005, in forum: C++
    Replies:
    0
    Views:
    899
  2. Replies:
    5
    Views:
    348
  3. Replies:
    0
    Views:
    290
  4. sbq

    JAVA Filing related

    sbq, Oct 19, 2007, in forum: Java
    Replies:
    6
    Views:
    400
    Roedy Green
    Oct 26, 2007
  5. GANGADHAR

    iConnect Group filing H1B for 2008

    GANGADHAR, Jan 9, 2008, in forum: Java
    Replies:
    0
    Views:
    404
    GANGADHAR
    Jan 9, 2008
Loading...

Share This Page