I can not delete a file in c ++

1

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;
}
    
asked by Max 29.11.2018 в 05:34
source

2 answers

0

I do not know exactly what is missing in your code but just yesterday I made one that works for me and you can base yourself on it, if it works, do not forget to publish it, greetings.

//Eliminar tabla
char nombre[20];
char alfabeto[26];
char Cifrado[26];
char aux[20];
ifstream salida;
salida.open("Tablas.txt",ios::in);
ofstream entrada;
entrada.open ("Temporal.txt",ios::out);

if(salida.fail()){
    cout<<"Hubo un error al abrir el archivo Tablas.txt";
    getch();
}else{
    cout<<"introduzca el nombre de la tabla que desea eliminar: ";
    cin>>aux;
    salida>>nombre;
    if(strcmp(aux,nombre)){
        cout<<"La tabla no existe"<<endl;
        }
    while(!salida.eof()){
        salida>>Cifrado;
        if(strcmp(aux,nombre)==0){
            cout<<"La tabla se ha Eliminado"<<endl;
            system("pause");
        }else{
            entrada<<nombre<<" "<<Cifrado<<endl;
        }
        salida>>nombre;
    }
}
entrada.close();
salida.close();

remove("Tablas.txt");
rename("Temporal.txt","Tablas.txt");
    
answered by 01.12.2018 в 04:28
0

Hello max your error is that before calling the functions remove and rename you must close all the objects that have opened the file to be deleted and rename, close Temp and show but not close save. Your problem is solved simply with this instruction just before calling the methods remove and rename:

Guardar.close();
    
answered by 02.12.2018 в 05:01