Título: Problema creando archivo auxiliar para eliminar datos en manejo de datos con c++
Publicado por: Abril7 en 11 Septiembre 2017, 16:32 pm
Hola, estoy haciendo un codigo que captura, muestra, elimina, modifica y busca datos en un archivo, este a su vez incluye un campo que es otro archivo, son 2 clases, una esta hecha con delimitadores y la otra con campos de dimension. El capturar, mostrar y buscar estan funcionando, y el eliminar casi, pero tengo una duda, en la funcion eliminar no se esta creando mi archivo auxiliar para respaldar los datos nuevos sin el registro que queria eliminar y no entiendo por que si para la otra clase si funciona y se crea el archivo, agradeceria mucho si alguien me puede decir que hago mal. MUCHAS GRACIAS. Estas son mis clases y librerias: #include <iostream> #include <fstream> #include <string.h> #include <cstring> using namespace std;
class Profile {
public: string captureP(); string showP(); void deleteUP(); void searchUP(); void editP();
char idProfile[30]; char level[30]; char type[30]; };
class User: public Profile {
public: void capture(); void show(); void deleteU(); void searchU(); void edit();
char idUser[30]; char name[30]; char mail[40]; char tel[20]; char ranking[30]; char idProfile[30]; }; Esa es mi funcion eliminar: void User::deleteU() { int op = 0; int ii,ll,tt; cout<<"What do you want to do? 1)Search by IDUSER 2) Search by IDPROFILE"<<endl; cin>>op;
if(op == 1) { string del; cout<<"Which idUser do you want to find?"<<endl; cin.ignore(); getline(cin,del); bool found = false;
ifstream read; ifstream read2; ofstream aux1; ofstream aux2;
int i = 0; int es = 0; aux1.open("Aux.txt",ios::out); aux2.open("Aux2.txt",ios::out); read.open("User.txt", ios::in); read2.open("Profile.txt", ios::in); char line[200]; read.getline(line,sizeof(line)); while(!read.eof()) { char *pointer; ii = strlen(idProfile); ll = strlen(level); tt = strlen(type);
read2.read((char *)&ii,sizeof(int)); read2.read((char *)&idProfile,ii); idProfile[ii]='\0'; read2.read((char *)&ll,sizeof(int)); read2.read((char *)&level,ll); level[ll]='\0'; read2.read((char *)&tt,sizeof(int)); read2.read((char *)&type,tt); type[tt]='\0';
for(int i=0; i<6; i++) { if(i==0) { pointer = strtok(line,"|"); strcpy(idUser,pointer); if(del == idUser) { es=1; } } else if(i==1) { pointer = strtok(NULL,"|"); strcpy(name,pointer); } else if(i==2) { pointer = strtok(NULL,"|"); strcpy(mail,pointer); } else if(i==3) { pointer = strtok(NULL,"|"); strcpy(tel,pointer); } else if(i==4) { pointer = strtok(NULL,"|"); strcpy(ranking,pointer); } else if(i==5) { pointer = strtok(NULL,"|"); //strcpy(idProfile,pointer);
} } if(es == 0) { aux2<<idUser<<'|'<<name<<'|'<<mail<<'|'<<tel<<'|'<<ranking<<'|'<<'\n';
cout<<" si entra 2"<<endl; aux1.write((char *)&ii,sizeof(int)); aux1.write((char *)&idProfile,ii); aux1.write((char *)&ll,sizeof(int)); aux1.write((char *)&level,ll); aux1.write((char *)&tt,sizeof(int)); aux1.write((char *)&type,tt);
} if(es == 1) { ///////////////////////////// while(!read2.eof() && found == false) { cout<<"____________DELETED______________"<<endl<<endl; cout<<"ID USER: "<<idUser<<endl; cout<<"NAME: "<<name<<endl; cout<<"MAIL: "<<mail<<endl; cout<<"TEL: "<<tel<<endl; cout<<"RANKING: "<<ranking<<endl;
cout<<"Id profile: "<<idProfile<<endl; cout<<"Level: "<<level<<endl; cout<<"Type: "<<type<<endl; cout<<"___________________________________"<<endl; es = 0; //_________________________________________________ //________________________________________________ found = true; }
if(read.eof()) { cout<<"File not found"<<endl; break; } ///////////////////////// cout<<"_________________________________"<<endl<<endl; es = 0; found = true; }
read.getline(line,sizeof(line)); if(read.eof() && found == false ) { cout<<"There isn't a file with this ID."<<endl; } } read.close(); read2.close(); aux1.close(); aux2.close(); //remove("Profile.txt"); remove("User.txt"); //rename("Aux.txt","Profile.txt"); rename("Aux2.txt","User.txt"); } else if(op == 2) { string word; cout<<"Type the idProfile that you want to find: "<<endl; cin>>word;
int i,l,t; ifstream read ("Profile.txt"); ofstream aux2; aux2.open("Aux2.txt",ios::out); bool found = false;
if (!read.good()) { cout<<"\n\n\tFile not found."<<endl; } else { while(!read.eof() && found == false) { read.read((char *)&i,sizeof(int)); read.read((char *)&idProfile,i); idProfile[i]='\0'; read.read((char *)&l,sizeof(int)); read.read((char *)&level,l); level[l]='\0'; read.read((char *)&t,sizeof(int)); read.read((char *)&type,t); type[t]='\0';
if(idProfile != word){ i = strlen(idProfile); l = strlen(level); t = strlen(type);
aux2.write((char *)&i,sizeof(int)); aux2.write((char *)&idProfile,i); aux2.write((char *)&l,sizeof(int)); aux2.write((char *)&level,l); aux2.write((char *)&t,sizeof(int)); aux2.write((char *)&type,t); }
if(idProfile == word) { cout<<"______________FOUND_______________"<<endl; cout<<"Id profile: "<<idProfile<<endl; cout<<"Level: "<<level<<endl; cout<<"Type: "<<type<<endl; cout<<"___________________________________"<<endl; found = true; }
if(read.eof()) { cout<<"File not found"<<endl; break; }
} read.close(); } } }
Título: Re: Problema creando archivo auxiliar para eliminar datos en manejo de datos con c++
Publicado por: AlbertoBSD en 12 Septiembre 2017, 18:41 pm
Bueno realmente no se en que punto te este fallando pero segun [1] If the stream is already associated with a file (i.e., it is already open), calling this function fails. Entonces, en Tienes un aux2 que nunca es cerrado, podrias empezar por ahi, cada open debe de tener un close. Saludos!
[1] http://www.cplusplus.com/reference/fstream/ofstream/open/
|