Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How to use reinterpret_cast in filling (http://www.velocityreviews.com/forums/t632554-how-to-use-reinterpret_cast-in-filling.html)

Husyn Raj 08-27-2008 08:34 AM

How to use reinterpret_cast in filling
 
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::out|ios::in|i os::binary);
do{
cout<<"Enter person data\n";
pers.getdata();
file.write(reinterpret_cast<char*>(&pers),sizeof(p ers));
cout<<"Another person (y/n)";
cin>>ch; }while(ch=='y');
file.seekg(0);
file.read(reinterpret_cast<char*>(&pers),sizeof(pe rs));
pers.showdata();
file.read(reinterpret_cast<char*>(&pers),sizeof(pe rs));
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...

Obnoxious User 08-27-2008 08:53 AM

Re: How to use reinterpret_cast in filling
 
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::out|ios::in|i os::binary); do{
> cout<<"Enter person data\n";
> pers.getdata();
> file.write(reinterpret_cast<char*>(&pers),sizeof(p ers));
> cout<<"Another person (y/n)";
> cin>>ch; }while(ch=='y');
> file.seekg(0);
> file.read(reinterpret_cast<char*>(&pers),sizeof(pe rs));
> pers.showdata();
> file.read(reinterpret_cast<char*>(&pers),sizeof(pe rs));
> 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::ostream & operator<<(std::ostream & 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

Muzammil 08-28-2008 07:53 AM

Re: How to use reinterpret_cast in filling
 
we need in this program to read iteratively using reinterpret_cast.
while(!file.eof())
{
file.read(reinterpret_cast<char*>(&pers),sizeof(pe rs));
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(pe rs));
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.

Obnoxious User 08-28-2008 09:05 AM

Re: How to use reinterpret_cast in filling
 
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(pe rs)); 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(pe rs)); 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

Muzammil 08-28-2008 06:06 PM

Re: How to use reinterpret_cast in filling
 
#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(pe rs));
cout<<"Another Person(y/n) ";
cin>>ch;
} while (ch=='y');
// out.
// out.close();

out.open("person.dat",fstream::binary | fstream::out);
out.seekp(0);
while(!out.eof())
{
out.read(reinterpret_cast<char*>(&pers),sizeof(per s));
pers.showdata();
}
out.close();
system("pause");
return 0;
}

Obnoxious User 08-28-2008 06:14 PM

Re: How to use reinterpret_cast in filling
 
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(pe rs));
> 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::out);


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(per s));


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

red floyd 08-28-2008 07:48 PM

Re: How to use reinterpret_cast in filling
 
On Aug 28, 12:53*am, Muzammil <muzammilPeer...@gmail.com> wrote:
> we need in this program to read iteratively using reinterpret_cast.
> while(!file.eof())
> {
> file.read(reinterpret_cast<char*>(&pers),sizeof(pe rs));
> 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(pe rs));
> 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-lit...html#faq-15..5


All times are GMT. The time now is 06:03 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.