C++/C++

Advertisement


Question
data reading from file
displays
twice(last record)
please help
Code Is HERE-----------
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class trys
{
  public:
char data[12];
int a;

  getval()
  {
  cout<<"\nenter charcter - ";cin>>data;
  cout<<"\nenter integer - ";cin>>a;
  
  }

  void show()
  {
  
  cout<<"\n"<<"    "<<data<<"  "<<a;   
  
  }

  

};

write()
{

trys b;
char ch;
ofstream out;
out.open("boys",ios::app | ios::out);
cout<<"\nenter the following information - ";
for(;;)
{
b.getval();
out.write((char *)&b,sizeof(b));
//out<<"\n";
cout<<"\nwant to more - ";
fflush(stdin);
cin>>ch;
if(ch=='y')
break;
}
out.close();

}

display()
{

trys b;
int sum=0;
ifstream in;
in.open("boys",ios::in);
cout<<"\the information - ";
while(1)
{

in.read((char *)&b,sizeof(b));
sum+=b.a;
b.show();
if(in.eof()!=NULL)
{
break;
}
}
cout<<"\nthe sum is - "<<sum-b.a;
in.close();
}


main()
{

write();

display();
}

Answer
> #include<fstream.h>
> #include<conio.h>
> #include<stdio.h>

These are not C++ headers; <conio.h> is not even a standard C header.
Use <fstream>, <cstdio> instead.


> in.read((char *)&b,sizeof(b));
If you want to perform binary i/o, open the stream in binary mode.

Here are a couple of tutorials on C++ i/o:
http://www.cplusplus.com/doc/tutorial/basic_io/
http://www.cplusplus.com/doc/tutorial/files/  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++09. i would not answer questions about gui and web programming.

Experience

over 15 years

Education/Credentials
post graduate engineer

©2012 About.com, a part of The New York Times Company. All rights reserved.