C++/C++
Expert: vijayan - 12/21/2010
Questiondata 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/