Tuesday, 9 April 2013

double CoinStack::value()


double CoinStack::value(){
  double value = 0.0;
  Coin C;
  open(_fname, ios::out|ios::binary);
  if(!fail()){
    while(!IsEmpty()){
      C = pop();
      value += (int)C;
      write((const char*)&C, sizeof(C));
    }
  }
  close();
  open(_fname, ios::in|ios::binary);
  if(!fail()){
    seekg(0, ios::end);
    int curloc = (int)tellg() - sizeof(Coin);
    while(curloc >= 0){
      seekg((ios::pos_type)curloc);
      read((char*)&C, sizeof(C));
      push(C);
      curloc -= sizeof(C);
    }
  }
  close();
  clear();
  open(_fname, ios::out|ios::trunc);
  close();
  return value;
}

No comments:

Post a comment