Hey there,
I have an error in file handling. When i use open function with ios::binary mode, it says-
no matching function for call to `std::basic_fstream<char, std::char_traits<char> >:pen(const char[8], bool)'
here are the codes. 2 files, one for entering data into file, one for deleting records.
the following is for writing records to file
Code:
#include<iostream.h>#include<fstream.h>
class employee
{
int code;
char name[20];
char desig[15];
float salary;
public: void get_emp();
};
void employee:: get_emp()
{
cout<<"Code--> ";
fflush(stdin);
cin>>code;
cout<<"Name--> ";
fflush(stdin);
gets(name);
cout<<"Designation--> ";
fflush(stdin);
gets(desig);
cout<<"Salary--> ";
fflush(stdin);
cin>>salary;
}
int main()
{
fstream f;
char ch;
f.open("EMP.dat", ios::binary||ios::app);
employee emp;
cout<<"Enter data:\n";
do
{
emp.get_emp();
f.write((char*)&emp,sizeof(emp));
cout<<"Entre more???(y/n)";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
f.close();
system("pause>null");
return 0;
}
the following is for deleting records
Code:
#include<iostream.h>#include<fstream.h>
#include<stdlib.h>
struct employee
{
int code;
char name[20];
char desig[15];
float salary;
}emp;
int main()
{
int xcode; //temporary declaration for employee code
int flag = 0;
fstream ef, tf;
//ef opened for reading, tf opened for transferring all records including modified record
ef.open("EMP.dat", ios::binary|| ios::in);
tf.open("TEMP.dat", ios::binary|| ios::out);
cout<<"Enter employee code to delete:";
cin>>xcode;
while(ef)
{
if(!ef)
exit(0);
ef.read((char*)&emp, sizeof(emp));
if(emp.code == xcode)
{
flag = 1;
}
else
tf.write((char*)&emp, sizeof(emp));
}
ef.close();
tf.close();
if(flag == 1)
cout<<"Record deleted.";
else
cout<<"Not found.";
fstream xf, yf;
//tf opened for reading
xf.open("TEMP.dat", ios::binary||ios::in);
//ef opened for copying all records from TEMP.dat
yf.open("EMP.dat",ios::binary||ios::out);
while(xf)
{
if(!xf)
exit(0);
xf.read((char*)&emp, sizeof(emp));
yf.write((char*)&emp, sizeof(emp));
}
xf.close();
yf.close();
system("pause");
return 0;
}
Please help,
thank you