I need to delete the original file and leave the other file copy to rename it with the name of the original file, but I do not know why it does not work for me.
I would appreciate your help.
remove("Fichero.txt");
rename("Temp.txt","Fichero.txt");
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[]) {
int clave, Bclave, opcion;
char nombre[30];
ofstream Guardar;
ifstream mostrar;
ofstream Temp;
Guardar.open("Fichero.txt",ios::app);
while(true){
cout<<"Ingresar opcion"<<endl;
cout<<"1 Ingresar datos"<<endl;
cout<<"2 Mostrar datos"<<endl;
cout<<"3 Buscar datos"<<endl;
cout<<"4 Eliminar datos"<<endl;
Temp.close();
mostrar.close();
cin>>opcion;
switch(opcion){
case 1:{
cout<<"Ingrese el nombre"<<endl;
cin>>nombre;
cout<<"Ingrese la clave"<<endl;
cin>>clave;
Guardar<<nombre<<" "<<clave<<endl;
break;
}
case 2:{
mostrar.open("Fichero.txt");
mostrar>>nombre;
while(!mostrar.eof()){
mostrar>>clave;
cout<<"Nombre "<<nombre<<endl;
cout<<"Clave "<<clave<<endl;
cout<<endl;
mostrar>>nombre;
}
mostrar.close();
break;
}
case 3:{
mostrar.open("Fichero.txt");
mostrar>>nombre;
bool verificar=false;
cout<<"Ingrese la clave que desea buscar"<<endl;
cin>>Bclave;
while(!mostrar.eof()){
mostrar>>clave;
if(clave==Bclave){
verificar=true;
cout<<endl;
cout<<"Nombre "<<nombre<<endl;
cout<<"Clave "<<clave<<endl<<endl;
}
mostrar>>nombre;
}
if(verificar==false){
cout<<"Clave no encontrada "<<endl;
}
mostrar.close();
break;
}
case 4:{
mostrar.open("Fichero.txt");
Temp.open("Temp.txt");
mostrar>>nombre;
bool verificar=false;
cout<<"Ingrese la clave que desea buscar"<<endl;
cin>>Bclave;
while(!mostrar.eof()){
mostrar>>clave;
if(clave==Bclave){
verificar=true;
cout<<endl;
cout<<"Nombre "<<nombre<<endl;
cout<<"Clave "<<clave<<endl<<endl;
cout<<"Datos eliminados";
}
else{
Temp<<nombre<<" "<<clave<<endl;
}
mostrar>>nombre;
}
if(verificar==false){
cout<<"Clave no encontrada "<<endl;
}
Temp.close();
mostrar.close();
remove("Fichero.txt");
rename("Temp.txt","Fichero.txt");
break;
}
}
}
return 0;
}