I am running this code but getting an undesirable output. The binary file cannot be opened.
#include<iostream>
#include<fstream>
using namespace std;
#include<stdio.h>
#include<string.h>
#include<process.h>
class CAMERA{
long ModelNo;
float MegaPixel;
int Zoom;
char Details[120];
public:
void Enter();
void Display();
long GetModelNo(){return ModelNo;
}
};
void CAMERA::Enter(){
cout<<"\nEnter Model No:";
cin>>ModelNo;
cout<<"\nEnter Resolution:";
cin>>MegaPixel;
cout<<"\nEnter Zoom:";
cin>>Zoom;
cout<<"\nEnter Details:\n";
cin>>Details;
cout<<"\n\n";
}
void CAMERA::Display(){
cout<<"\nModel No:";
cout<<ModelNo;
cout<<"\nResolution:";
cout<<MegaPixel;
cout<<"\nZoom:";
cout<<Zoom;
cout<<"\nDetails:\n";
cout<<Details;
cout<<"\n\n";
}
void search(long MNo){
int found;
ifstream ifile;
ifile.open("CAMERA.bin",ios::in | ios::binary);
if(ifile.is_open()){
CAMERA c;
found=0;
while(ifile.read((char *) &c, sizeof(c))){
if(c.GetModelNo()==MNo){
c.Display();
found=1;
break;
}
}
}
else{
cout<<"\ncouldn't open file\n";
exit(-1);
}
if(found==0)
cout<<"\nGiven Model No. not found\n";
ifile.close();
}
void create(){
ofstream ifile("CAMERA.bin",ios::in | ios::binary);
if(!ifile){
cout<<"\ncould not open the file\n";
return;
}
CAMERA c;int n;
cout<<"Enter No. of Records:";
cin>>n;
cout<<"\n\n";
for(int i=1;i<=n;i++){
c.Enter();
ifile.write((char*)&c,sizeof(c));
}
ifile.close();
}
int main(){
create();
search(101);
}