You are here:

C++/c++ problem

Advertisement


Question
I had problems with data handling in c++.
When i use read function to read from a file and show it on screen, the last record in the file appears thrice.
I use the read function in a class.  

Answer
I would like to demonstrate the use of read using an example:

struct details
{
int id;
char[100] name;
}det;
main()
{
strcpy(det.name,"SaiRam");
det.id = 100;

offstream outf("fileopened",ios::out|ios::binary);
if(!outf)
{
cout<<"sorry the file can't be opened for output";
}
outf.write((*char)&det,sizeof(struct details));
ifstream inf("fileopened",ios::in|ios::binary);
if(!inf)
{
cout<<"sorry the file can't be opened for input";
}
inf.read((*char)&det,sizeof(struct details));
cout<<"name"<<det.name;
cout<<"id"<<det.id;
}

Please observe the way the file has been opened for output to copy the details from the strucutre into the file and how the details have been copied back into the file.

Is this what u require? If not please rivert back .

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Saikrishna

Expertise

I can answer questions in C (Basic and a little of advanced). I can't answer questions related to multi threading

Experience

2.6 years

Education/Credentials
I am a B-tech graduate majored in computer science.

©2016 About.com. All rights reserved.