Sorting of records in the file
Hello friends
First of all i would like to thanks all who helped me for solving my password program problem in c++.I had a two problems 1.searching 2.sorting of records in the file from which first problem of searching the records is solved but Now i have a problem that's how to short a records in the file in c++.I also want to ask u Is there are any online full preview c++ book with free of cost which can i read for better understanding of c++.Try to understand my problem through Example.i downloaded the program the internet when i don't come to solution but this program also not working and is showing two errors.
#include<fstream.h>
class emp
{
private:
int empno;
char name [20];
float salary;
public:
void get();
void display();
};
void emp::get()
{
cin>>empno>>name>>salary;
}
void emp::display()
{
cout<<empno<<name<<salary<<"\n";
}
void show_recs(ifstream&);
int no_of_recs(ifstream&);
int main (void)
{
ifstream fin;
ofstream fout;
emp *ep,e,t;
int position,i,j,nor;
fin.open("emp.dat");
cout<<"\n Unsorted List of Employees \n";
show_recs(fin);
nor=no_of_recs(fin);
ep=new emp[nor];
i=0;
fin.seekg(0);
while(fin.read((char*)&e,sizeof(e)))
{
ep=e;
i++;
}
fin.close();
/* sorting begins */
for(i=0; i < nor; i++)
for(j=i+1; j < nor; j++)
if (ep.salary>ep[j].salary) //here two error showing 1. sorting.cpp 50: 'emp::salary' is is not accesible. 2.sorting.cpp 50: 'emp::salary' is not accesible
{
t=ep;
ep=ep[j];
ep[j]=t;
}
/* sorting ends */
fout.open("emps.dat");
for (i=0;i< nor;i++)
fout.write((char*)&ep,sizeof(ep));
fout.close();
cout<<"\n Sorted List of Employees in the increaseing order of salary \n";
fin.open("emps.dat");
show_recs(fin);
return 0;
}
int no_of_recs(ifstream& fin)
{
int last_byte,nor;
fin.seekg(0,ios::end);
last_byte=fin.tellg();
nor=last_byte/ sizeof(emp);
return nor;
}
void show_recs(ifstream& fin)
{
emp e;
while(fin.read((char*)&e,sizeof(e)))
e.display();
fin.clear();
}
First of all i would like to thanks all who helped me for solving my password program problem in c++.I had a two problems 1.searching 2.sorting of records in the file from which first problem of searching the records is solved but Now i have a problem that's how to short a records in the file in c++.I also want to ask u Is there are any online full preview c++ book with free of cost which can i read for better understanding of c++.Try to understand my problem through Example.i downloaded the program the internet when i don't come to solution but this program also not working and is showing two errors.
#include<fstream.h>
class emp
{
private:
int empno;
char name [20];
float salary;
public:
void get();
void display();
};
void emp::get()
{
cin>>empno>>name>>salary;
}
void emp::display()
{
cout<<empno<<name<<salary<<"\n";
}
void show_recs(ifstream&);
int no_of_recs(ifstream&);
int main (void)
{
ifstream fin;
ofstream fout;
emp *ep,e,t;
int position,i,j,nor;
fin.open("emp.dat");
cout<<"\n Unsorted List of Employees \n";
show_recs(fin);
nor=no_of_recs(fin);
ep=new emp[nor];
i=0;
fin.seekg(0);
while(fin.read((char*)&e,sizeof(e)))
{
ep=e;
i++;
}
fin.close();
/* sorting begins */
for(i=0; i < nor; i++)
for(j=i+1; j < nor; j++)
if (ep.salary>ep[j].salary) //here two error showing 1. sorting.cpp 50: 'emp::salary' is is not accesible. 2.sorting.cpp 50: 'emp::salary' is not accesible
{
t=ep;
ep=ep[j];
ep[j]=t;
}
/* sorting ends */
fout.open("emps.dat");
for (i=0;i< nor;i++)
fout.write((char*)&ep,sizeof(ep));
fout.close();
cout<<"\n Sorted List of Employees in the increaseing order of salary \n";
fin.open("emps.dat");
show_recs(fin);
return 0;
}
int no_of_recs(ifstream& fin)
{
int last_byte,nor;
fin.seekg(0,ios::end);
last_byte=fin.tellg();
nor=last_byte/ sizeof(emp);
return nor;
}
void show_recs(ifstream& fin)
{
emp e;
while(fin.read((char*)&e,sizeof(e)))
e.display();
fin.clear();
}
0