Monday 10 December 2012

Search in file

To search for the employee record on the file:


int search (fstream &f , char* name, Employee &E){

    Employee emp;
    int loc;

   f.seekg( 0 , ios::end);

  loc=f.tellg();

while( loc > 0 && !found){
        loc -= sizeof(emp);
        f.seekg(loc);

     f.read((char*)emp , sizeof(emp));
   
    if (emp.name() == E.Name()){
        E = emp ;
        found = true;
}
return found ;
}
  
      

Test Two Q4:


The answer for the question 4 for test two:


 templat <class T>class SQueue : public Queue<T>, public fstream {
             char* _fname;



public: SQueue(
const char* fname):Queue(),fstream(fname, ios::binary|ios::in|ios::out) {
 


    _fname = new char[strlen(fname) + 1];

     strcpy(_fname, fname);

     open(fname, ios::binary|ios::in);

      seekg((ios::pos_type)0 ,ios::beg);

     T t;

        while (read((char*)&t,sizeof(T))) {

         Queue::Add(t);

}

    close();

}

~SQueue() {

T t;

open(_fname, ios::out|ios::binary );

seekp((ios::pos_type)0 ,ios::beg);
while (t=Remove()) {


write((char*)t,sizeof(T));

}

close();

}

};

intmain() {

 
SQueue<int> sq("c:\atieh\test.bin");

}