void BaseDeDatos::cargarBufferDesdeArchivo(){ fstream bdfile; bdfile.open(ARCHIVO_BASE, ios_base::in | ios_base::binary); if (!bdfile.is_open()){ bdfile.open(ARCHIVO_BASE, ios_base::in| ios_base::binary | ios_base::trunc); } while(!bdfile.eof() && !this->bufferLleno()) { Registro r; t_registro tr; if(bdfile.read((char*)(&tr), Registro::getSize()) ){ r.crearDesdeRegistro(tr); this->bufferRegistros.push_back(r); } else { break; } } bdfile.close(); }
int BaseDeDatos::modificar(Registro& exist, const Registro& modif){ if (!this->existente(exist)){ return ERR_NO_EXISTE; } vector<Registro>::iterator it = currrentPosition; if (this->existente(modif) && !exist.compararPorNombre(modif)){ return ERR_DUPLICADO; } exist.llenarVacios(*it); Registro nuevo = modif; nuevo.llenarVacios(*it); /* Modificar en cache */ it = this->bufferRegistros.insert(it, nuevo); if ((it+1) != this->bufferRegistros.end()){ this->bufferRegistros.erase(it+1); } currrentPosition = it; /* Modificar en el archivo */ t_registro reg = nuevo.getRegistroASerializar(); t_registro rbusq; Registro busq; bool encontrado = false; fstream bdfile; bdfile.open (ARCHIVO_BASE, ios_base::out | ios_base::in | ios_base::binary); while(!bdfile.eof() && !encontrado){ bdfile.read((char*)&rbusq, Registro::getSize()); busq.crearDesdeRegistro(rbusq); if (exist.compararPorNombre(busq)){ bdfile.seekp(- Registro::getSize(), ios_base::cur); bdfile.write((char*)®, Registro::getSize()); encontrado = true; } } bdfile.close(); return SUCCESS; }