How to use reinterpret_cast in filling

Discussion in 'C++' started by Husyn Raj, Aug 27, 2008.

  1. Husyn Raj

    Husyn Raj Guest

    class person
    { protected:
    char name[80];
    int age;
    public:
    void getdata()
    {cout<<"Enter name";
    cin>>name;
    cout<<"Enter age";
    cin>>age;
    }
    void showdata()
    {cout<<"Name "<< name;
    cout<<"Age "<< age;
    } };
    char ch;
    person pers;
    fstream file;
    file.open("person.txt",ios::app|ios::eek:ut|ios::in|ios::binary);
    do{
    cout<<"Enter person data\n";
    pers.getdata();
    file.write(reinterpret_cast<char*>(&pers),sizeof(pers));
    cout<<"Another person (y/n)";
    cin>>ch; }while(ch=='y');
    file.seekg(0);
    file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
    pers.showdata();
    file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
    pers.showdata();
    file.close();
    cout<<endl;
    system("pause");
    return 0;}


    this is my code and pers.showdata(); is only showing the last data
    entered how to do this properly...
     
    Husyn Raj, Aug 27, 2008
    #1
    1. Advertising

  2. On Wed, 27 Aug 2008 01:34:10 -0700, Husyn Raj wrote:

    > class person
    > { protected:
    > char name[80];
    > int age;
    > public:
    > void getdata()
    > {cout<<"Enter name";
    > cin>>name;
    > cout<<"Enter age";
    > cin>>age;
    > }
    > void showdata()
    > {cout<<"Name "<< name;
    > cout<<"Age "<< age;
    > } };
    > char ch;
    > person pers;
    > fstream file;
    > file.open("person.txt",ios::app|ios::eek:ut|ios::in|ios::binary); do{
    > cout<<"Enter person data\n";
    > pers.getdata();
    > file.write(reinterpret_cast<char*>(&pers),sizeof(pers));
    > cout<<"Another person (y/n)";
    > cin>>ch; }while(ch=='y');
    > file.seekg(0);
    > file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
    > pers.showdata();
    > file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
    > pers.showdata();
    > file.close();
    > cout<<endl;
    > system("pause");
    > return 0;}
    >
    >
    > this is my code and pers.showdata(); is only showing the last data
    > entered how to do this properly...


    If you need serialization, I would recommend you to add these
    two functions instead of relying on reinterpret_cast<>.

    std::eek:stream & operator<<(std::eek:stream & out, person const & p) {
    // example
    out << p.get_name() << " " << p.get_age() << std::endl;
    return out;
    }
    std::istream & operator>>(std::istream & in, person & p) {
    // example
    std::string name;
    in >> name;
    p.set_name(name);
    int age;
    in >> age;
    p.set_age(age);
    return in;
    }

    Let the io streams to all the magic.

    --
    OU
     
    Obnoxious User, Aug 27, 2008
    #2
    1. Advertising

  3. Husyn Raj

    Muzammil Guest

    we need in this program to read iteratively using reinterpret_cast.
    while(!file.eof())
    {
    file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
    pers.showdata();
    }

    this cause problem in reading data from file and remain in loop.and
    dont terminate.
    main problem is that we are using fstream in dev c++. and i think
    dev c++ dont support to much to fstream.
    b/c it requires ifstream or ofstream objects.

    in this program.
    we first write data to file using "file" object and then we have
    done of writing process we want to re-open this file for reading
    purpose with same object "file".
    for reading we move pointer to initial position of file .then read
    like this.
    while(!file.eof())
    {
    file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
    pers.showdata();
    }
    which read the data at some intance and dont come out from loop.that
    big problem why this loop dont break.when file is end.
     
    Muzammil, Aug 28, 2008
    #3
  4. On Thu, 28 Aug 2008 00:53:33 -0700, Muzammil wrote:

    > we need in this program to read iteratively using reinterpret_cast.
    > while(!file.eof())
    > {
    > file.read(reinterpret_cast<char*>(&pers),sizeof(pers)); pers.showdata();
    > }
    >
    > this cause problem in reading data from file and remain in loop.and
    > dont terminate.
    > main problem is that we are using fstream in dev c++. and i think dev
    > c++ dont support to much to fstream. b/c it requires ifstream or
    > ofstream objects.
    >


    Nonsense.

    > in this program.
    > we first write data to file using "file" object and then we have done
    > of writing process we want to re-open this file for reading purpose
    > with same object "file".
    > for reading we move pointer to initial position of file .then read
    > like this.
    > while(!file.eof())
    > {
    > file.read(reinterpret_cast<char*>(&pers),sizeof(pers)); pers.showdata();
    > }
    > which read the data at some intance and dont come out from loop.that
    > big problem why this loop dont break.when file is end.


    Post a minimal proper compilable program that shows the error,
    not just pieces without context.

    --
    OU
     
    Obnoxious User, Aug 28, 2008
    #4
  5. Husyn Raj

    Muzammil Guest

    #include <iostream>
    #include <fstream>
    using namespace std;
    class person
    {
    protected:
    char name[80];
    int age;
    public:
    void getdata()
    {
    cout<<"Enter Name";cin>>name;
    cout<<"Age";cin>>age;
    }
    void showdata()
    {
    cout<<"Name:"<<name<<endl;
    cout<<"Age:"<<age<<endl;
    }
    };
    int main()
    { char ch;
    person pers;
    fstream out;
    out.open("person.dat");

    do
    {
    cout<<"Enter Person Data"<<endl;
    pers.getdata();
    out.write(reinterpret_cast<char*>(&pers),sizeof(pers));
    cout<<"Another Person(y/n) ";
    cin>>ch;
    } while (ch=='y');
    // out.
    // out.close();

    out.open("person.dat",fstream::binary | fstream::eek:ut);
    out.seekp(0);
    while(!out.eof())
    {
    out.read(reinterpret_cast<char*>(&pers),sizeof(pers));
    pers.showdata();
    }
    out.close();
    system("pause");
    return 0;
    }
     
    Muzammil, Aug 28, 2008
    #5
  6. Quote some context!

    On Thu, 28 Aug 2008 11:06:03 -0700, Muzammil wrote:

    > #include <iostream>
    > #include <fstream>
    > using namespace std;
    > class person
    > {
    > protected:
    > char name[80];
    > int age;
    > public:
    > void getdata()
    > {
    > cout<<"Enter Name";cin>>name;
    > cout<<"Age";cin>>age;


    And what if I enter a letter as age?
    Verify the stream after input.

    > }
    > void showdata()
    > {
    > cout<<"Name:"<<name<<endl;
    > cout<<"Age:"<<age<<endl;
    > }
    > };
    > int main()
    > { char ch;
    > person pers;
    > fstream out;


    fstream is for both in and out, thus you do not need to
    try to reopen it below.

    > out.open("person.dat");
    >


    You're writing binary data, but you open the stream in
    text mode.

    out.open("person.dat", std::ios_base::binary);

    And you should check if it succeeds:

    if(!out) {
    std::cout<<"failed"<<std::endl;
    return 0;
    }

    > do
    > {
    > cout<<"Enter Person Data"<<endl;
    > pers.getdata();
    > out.write(reinterpret_cast<char*>(&pers),sizeof(pers));
    > cout<<"Another Person(y/n) ";
    > cin>>ch;


    Verify the stream. What happens if I enter a digit instead?

    > } while (ch=='y');
    > // out.
    > // out.close();
    >
    > out.open("person.dat",fstream::binary | fstream::eek:ut);


    You should *always* check if open() succeeds

    if(!out) {
    std::cout<<"failed"<<std::endl;
    return 0;
    }

    > out.seekp(0);


    seekp() is used with write.
    seekg() is used with read.

    > while(!out.eof())


    Has the stream reached end of file? This is not the same as
    asking the question: is the stream still good()?

    > {
    > out.read(reinterpret_cast<char*>(&pers),sizeof(pers));


    Since trying to reopen it fails, this read fails too.

    > pers.showdata();
    > }
    > out.close();
    > system("pause");
    > return 0;
    > }


    Read about streams and how to check their status, and how
    to restart them again after some errors.

    --
    OU
     
    Obnoxious User, Aug 28, 2008
    #6
  7. Husyn Raj

    red floyd Guest

    On Aug 28, 12:53 am, Muzammil <> wrote:
    > we need in this program to read iteratively using reinterpret_cast.
    > while(!file.eof())
    > {
    > file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
    > pers.showdata();
    >
    > }
    >
    > this cause  problem in reading data from file and  remain in loop.and
    > dont terminate.
    > main problem is  that we are using fstream in dev c++. and  i think
    > dev c++ dont support to much to fstream.
    > b/c it  requires ifstream or  ofstream objects.
    >
    > in this program.
    > we first write data to file using  "file" object  and  then we have
    > done  of writing process we want to re-open this file for reading
    > purpose with same object "file".
    > for reading we move pointer  to  initial  position of file .then read
    > like this.
    > while(!file.eof())
    > {
    > file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
    > pers.showdata();}
    >


    That's because the loop doesn't do what you think it does.

    See FAQ 15.5 http://www.parashift.com/c -faq-lite/input-output.html#faq-15..5
     
    red floyd, Aug 28, 2008
    #7
    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. Alex Vinokur
    Replies:
    3
    Views:
    709
  2. Alex Vinokur
    Replies:
    1
    Views:
    612
  3. Replies:
    12
    Views:
    434
    Satish Kumar
    Apr 14, 2014
  4. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    147
    Mohsen Pahlevanzadeh
    Sep 9, 2013
  5. MRAB
    Replies:
    0
    Views:
    134
Loading...

Share This Page