ARCHIVOTEMPORALC++.CPP December 19, 2001 Page 1 #ifndef ARCHIVOTEMPORAL #define ARCHIVOTEMPORAL #include "F_Excepciones.h" #include <iostream.h> #include <stdlib.h> #include <iomanip.h> #include <fstream.h> #include <stdio.h> //Para la función unlink #define ERR -1 #define OK 0 class ArchivoTemporal { int tam_reg; char tempfich[15]; int num_reg; void CrearATMP(void); int CopiarATMP(const ArchivoTemporal &); int Rellenar(int); public: ArchivoTemporal(int); ~ArchivoTemporal() { unlink(tempfich); } ArchivoTemporal(const ArchivoTemporal &); ArchivoTemporal operator=(const ArchivoTemporal&); int Escribir(int, void *) throw (Excepcion_Error_Fichero); int Leer (int, void *) const throw (Excepcion_Error_Fichero); }; int ArchivoTemporal::CopiarATMP(const ArchivoTemporal &AT) { ofstream f1; ifstream f2; char *buffer; if(tam_reg!=AT.tam_reg) return ERR; buffer=new char [tam_reg]; if(!buffer) throw Excepcion_Sin_Memoria(); f1.open(tempfich, ios::out|ios::binary); if(!f1) throw Excepcion_Error_Fichero(EXC_APERTURA_LECTURA,tempfich); f2.open(AT.tempfich, ios::in|ios::binary); if(!f2) throw Excepcion_Error_Fichero(EXC_APERTURA_ESCRITURA,AT.tempfich); while (f2.read(buffer, tam_reg)) { f1.write(buffer, tam_reg); //f2.read(buffer, tam_reg); } delete []buffer; // Los destructores de f1 y f2 cierran los ficheros return OK; } ArchivoTemporal::ArchivoTemporal(int tam) { tam_reg=tam; num_reg=0; CrearATMP(); } ARCHIVOTEMPORALC++.CPP December 19, 2001 void ArchivoTemporal::CrearATMP(void) { int correcto=0; fstream fp; randomize(); do { sprintf(tempfich, "ATMP%d.tmp", rand()%10000); fp.open(tempfich, ios::in); if(!fp) { fp.open(tempfich, ios::out); correcto=1; } fp.close(); } while(!correcto); } ArchivoTemporal::ArchivoTemporal(const ArchivoTemporal &AT) { tam_reg=AT.tam_reg; num_reg=AT.num_reg; CrearATMP(); CopiarATMP(AT); } ArchivoTemporal ArchivoTemporal::operator=(const ArchivoTemporal &AT) { if (&AT!=this) { unlink(tempfich); tam_reg=AT.tam_reg; num_reg=AT.num_reg; CrearATMP(); CopiarATMP(AT); } return *this; } int ArchivoTemporal::Escribir(int pos, void *reg) throw (Excepcion_Error_Fichero) { fstream fp; if (pos>num_reg) Rellenar(pos); fp.open(tempfich, ios::in|ios::out|ios::binary); if(!fp) throw Excepcion_Error_Fichero(EXC_APERTURA_LECTURA,tempfich); fp.seekp(pos*(long)tam_reg); fp.write((char *)reg, tam_reg); return OK; } int ArchivoTemporal::Rellenar (int hasta) { fstream fp; char *buffer; buffer=new char [tam_reg]; if (!buffer) throw Excepcion_Sin_Memoria(); Page 2 ARCHIVOTEMPORALC++.CPP December 19, 2001 memset(buffer, ' ', tam_reg); fp.open(tempfich, ios::app | ios::binary); if(!fp) throw Excepcion_Error_Fichero(EXC_APERTURA_ESCRITURA,tempfich); for (int i=num_reg; i<=hasta; i++) fp.write(buffer, tam_reg); delete []buffer; num_reg=hasta+1; return OK; } int ArchivoTemporal::Leer(int pos, void *reg) throw (Excepcion_Error_Fichero) { ifstream fp; const if (pos>num_reg) return ERR; fp.open(tempfich, ios::in|ios::binary); if(!fp) throw Excepcion_Error_Fichero(EXC_APERTURA_LECTURA,tempfich); fp.seekg(pos*(long)tam_reg); fp.read((char *)reg, tam_reg); return OK; } #define ARCHIVOTEMPORAL_TEST #ifdef ARCHIVOTEMPORAL_TEST #define MAX_FRASE 30 void Escritura (ArchivoTemporal& A); void Lectura (ArchivoTemporal &A); void main(void) { ArchivoTemporal A(MAX_FRASE); int seguir=1; while(seguir) { cout<<"0. Salir"<<endl; cout<<"1. Escribir"<<endl; cout<<"2. Leer"<<endl; cin>>seguir; cin.sync(); try { switch(seguir) { case 1: Escritura(A); break; case 2: Lectura(A); break; } } catch(Excepcion_Sin_Memoria) { cerr<<"No es posible reservar memoria, se abortará el programa"; abort(); } } } void Escritura (ArchivoTemporal& A) { char frase [MAX_FRASE]; Page 3 ARCHIVOTEMPORALC++.CPP December 19, 2001 int posicion; try { cout<<"Deme una frase: "; cin.get(frase,MAX_FRASE); cout<<"¿En qué posición la guardamos?: "; cin>>posicion; cin.sync(); A.Escribir(posicion, frase); } catch(Excepcion_Error_Fichero E) { char nombre[50]; switch(E.codigo()) { case EXC_APERTURA_LECTURA: E.fichero(nombre); cerr<<"Error en la apertura para lectura"; cerr<<" del fichero:"<<nombre<<endl; break; case EXC_APERTURA_ESCRITURA: E.fichero(nombre); cerr<<"Error en la apertura para escritura"; cerr<<" del fichero:"<<nombre<<endl; break; } } } void Lectura (ArchivoTemporal &A) { char frase [MAX_FRASE]; int posicion; try { cout<<"Posicion de lectura: "; cin>>posicion; A.Leer(posicion, frase); cout<<"La frase leida es:"<<frase<<endl; } catch(Excepcion_Error_Fichero E) { char nombre[50]; switch(E.codigo()) { case EXC_APERTURA_LECTURA: E.fichero(nombre); cerr<<"Error en la apertura para lectura"; cerr<<" del fichero:"<<nombre<<endl; break; case EXC_APERTURA_ESCRITURA: E.fichero(nombre); cerr<<"Error en la apertura para escritura"; cerr<<" del fichero:"<<nombre<<endl; break; } } } #endif #endif Page 4