CrazyEngineers
  • Sorting of records in the file

    sumit_goel

    Member

    Updated: Oct 27, 2024
    Views: 1.2K
    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();
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • bill190

    MemberJul 25, 2010

    1. The file or data needs to be in some sort of "format". Common are called fixed length, and comma delimited. More...
    <a href="https://www.google.com/#hl=en&source=hp&q=comma+delimited+file&aq=f&aqi=g9g-m1&aql=&oq=&gs_rfai=C5MKEkjRMTIaZD4mgjQPXy9mMDwAAAKoEBU_Q1dWv&fp=6cbe660b28e9b05b" target="_blank" rel="nofollow noopener noreferrer">Google</a>

    2. Read about parsing data...
    <a href="https://www.google.com/#q=parse+data&hl=en&sa=2&fp=6cbe660b28e9b05b" target="_blank" rel="nofollow noopener noreferrer">Google</a>

    3. Sorting algorithms...
    <a href="https://www.google.com/#hl=en&q=Sorting+algorithms+C%2B%2B&aq=&aqi=g2&aql=&oq=Sorting+algorithms+C%2B%2B&gs_rfai=&fp=6cbe660b28e9b05b" target="_blank" rel="nofollow noopener noreferrer">Google</a>

    4. Sorting data is common with a database. Might want to read about how a database works. Also there is a thing called a "flat file database". More...
    <a href="https://www.google.com/#hl=en&q=flat+file+database&aq=0&aqi=g10&aql=&oq=flat+file+dat&gs_rfai=&fp=6cbe660b28e9b05b" target="_blank" rel="nofollow noopener noreferrer">Google</a>
    Are you sure? This action cannot be undone.
    Cancel
  • sumit_goel

    MemberJul 25, 2010

    Thank you friend for giving response to my problem of sorting the file. But i think you don't understand my problem fully actually i want to ask how to sort the records within the file using class and without pointer.I'll try to understand my problem through example and i hope u definitely understand my problem and 'll help me solving the problem
    Are you sure? This action cannot be undone.
    Cancel
  • Srikz

    MemberJul 26, 2010

    Not exactly answering your main question here. But as for where you can get full preview books, <a href="https://books.google.com/" target="_blank" rel="nofollow noopener noreferrer">Google Books</a> have always been a great help to me. Be it licensed or unlicensed, you get a preview of the books. Though only some pages are visible and most of the problems hidden, nevertheless its a great way to take a peek inside almost any book.
    Other than this, i'm afraid i do not know of any other preview sites that host licensed books.
    What i find helpful is, find the right book for me with the content i'm able to understand through Google Books then either buy it or search for topics in plain Google. Helps a lot.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register