How to use reinterpret_cast in filling

H

Husyn Raj

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...
 
O

Obnoxious User

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.
 
M

Muzammil

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.
 
O

Obnoxious User

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.
 
M

Muzammil

#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;
}
 
O

Obnoxious User

Quote some context!

#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.
 
R

red floyd

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top