Open main menu

CDOT Wiki β

Q2sol the solution

Revision as of 17:21, 4 August 2010 by Catherine.leung (talk | contribs) (Created page with ' <source lang="cpp"> void Process(fstream& fs,int recsize){ allocate memory so we can have a place to put data: char* rec=new char[recsize]; int numrec; /*find out …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


void Process(fstream& fs,int recsize){

  /*allocate memory so we can have a place to put data*/
  char* rec=new char[recsize];
  int numrec;

  /*find out how many records are in our file*/
  fs.seekg(0,ios::end);
  numrec=fs.tellg()/recsize;

  for(int i=0;i<numrec;i++){
    fs.seekg(i*recsize,ios::beg);
    fs.read((char*)rec,recsize);
    rec[recsize-2]=10;   //10 is LF  recsize -2 is where the CR was

    //make sure we are in correct place when we do our writes.
    fs.seekp(i*(recsize-1),ios::beg);
    fs.write((char*)rec,recsize-1);
  }
  delete [] rec;   //don't want memory leak so make sure mem is deallocated.
}