Remove() function does not delete the file in C

Asked

Viewed 88 times

1

I am trying to delete a file and soon after rename another file, but it turns out that the remove function does not erase the file. I tested on two machines, giving full permission, but without success. In debug I get the message permission danied, but I have already given full permission. The file I cannot delete is the puser = fopen("dados\\cad_funcionario.txt", "r"), i close it inside the last while of that code. What can be that?

    /*DELETE TELAS*/
void deletar_usuario        (void)
{

    int test;
    test = 0;
    
    user* cusuario = calloc(1, sizeof(user)* 1);
    user_del* cusuario_del = calloc(1, sizeof(user_del)* 1);

    puser = fopen("dados\\cad_funcionario.txt", "r");
    if (puser == NULL) {
        fprintf(stderr, "\n\nErro ao abrir arquivo ou ainda nao foi realizado nenhum cadastro!");
        
        int verificaarq;
        verificaarq = 0;
        printf("\n\nTecle [1] para voltar ao menu principal ou [2] para sair do programa: ");
        scanf("%i", &verificaarq);

        switch (verificaarq)
        {
            case 1 : main_principal();  break;  
            case 2 : exit(0);       
            default:printf("\nMenu inexistente, tente novamente [1] ou [2].");
        }
    }
    
    temporario = fopen("dados\\temporario_user_del.txt", "w");
    if (temporario == NULL) {
        fprintf(stderr, "\n\nErro ao abrir arquivo ou ainda nao foi realizado nenhum cadastro!");
        
        int verificaarq;
        verificaarq = 0;
        printf("\n\nTecle [1] para voltar ao menu principal ou [2] para sair do programa: ");
        scanf("%i", &verificaarq);

        switch (verificaarq)
        {
            case 1 : main_principal();  break;  
            case 2 : exit(0);       
            default:printf("\nMenu inexistente, tente novamente [1] ou [2].");
        }
    }

    printf("\033[2J\033[1;1H");
    printf("DELETAR USUARIO CADASTRADO\n\n");

    printf("Para deletar um usuario, insira o CPF do usuario: ");
    tratamento_cpf(cusuario_del->cpf, 11);

    while (fscanf(puser, "%d\n %[^\n] %[^\n] %[^\n] %[^\n] %[^\n] %[^\n] %[^\n] %[^\n] %[^\n] %[^\n] %[^\n] %[^\n]s", &cusuario->codigo, cusuario->nome, cusuario->celular, cusuario->email, cusuario->cpf, cusuario->usuario, cusuario->senha, cusuario->endereco.estado, cusuario->endereco.cidade, cusuario->endereco.bairro, cusuario->endereco.logradouro, cusuario->endereco.complemento, cusuario->endereco.cep)!=EOF)
    {
        if(strcmp(cusuario->cpf, cusuario_del->cpf) != 0)
        {
            fprintf(temporario, "%i\n", cusuario->codigo);
            fprintf(temporario, "%s\n", cusuario->nome);
            fprintf(temporario, "%s\n", cusuario->celular);
            fprintf(temporario, "%s\n", cusuario->email);
            fprintf(temporario, "%s\n", cusuario->cpf);
            fprintf(temporario, "%s\n", cusuario->usuario);
            fprintf(temporario, "%s\n", cusuario->senha);
            fprintf(temporario, "%s\n", cusuario->endereco.estado);
            fprintf(temporario, "%s\n", cusuario->endereco.cidade);
            fprintf(temporario, "%s\n", cusuario->endereco.bairro);
            fprintf(temporario, "%s\n", cusuario->endereco.logradouro);
            fprintf(temporario, "%s\n", cusuario->endereco.complemento);
            fprintf(temporario, "%s\n", cusuario->endereco.cep);
        }
        else
        {
            test++;
            memset(cusuario->senha, 0, sizeof(cusuario->senha));
            printf("\n\nCodigo: %i\nNome: %s\nCelular: %.2s-%.5s-%.4s\nE-mail: %s\nCPF: %.3s.%.3s.%.3s-%.2s\nUsuario no sistema: %s\nSenha: %s\nEstado: %s\nCidade: %s\nBairro: %s\nlogradouro: %s\nComplemento: %s\nCep: %.5s-%.3s\n", cusuario->codigo, cusuario->nome, cusuario->celular, cusuario->celular+2, cusuario->celular+7, cusuario->email, cusuario->cpf, cusuario->cpf+3, cusuario->cpf+6, cusuario->cpf+9, cusuario->usuario, cusuario->senha, cusuario->endereco.estado, cusuario->endereco.cidade, cusuario->endereco.bairro, cusuario->endereco.logradouro, cusuario->endereco.complemento, cusuario->endereco.cep, cusuario->endereco.cep+5);
        }
    }

    while((cusuario->salvar != 1 || cusuario->salvar != 2) && test == 0)
    {
        printf("\n\n\nNao ha registros com esse CPF!\n\n"); 
        printf("\n\nTecle [1] para tentar novamente ou [2] para VOLTAR ao menu de cadastro: ");
        scanf("%i", &cusuario->salvar);

        if (cusuario->salvar == 1 || cusuario->salvar == 2)
        {
            fclose(puser);
            fclose(temporario);
        }
        switch (cusuario->salvar)
        {
            case 1 : deletar_usuario(); break;  
            case 2 : main_delete(); break;
            default:printf("\nOpcao inexistente, tente novamente [1] ou [2]."); 
        }
    }

    while(cusuario->salvar != 1 || cusuario->salvar != 2)
    {
        printf("\n\nTecle [1] para confirmar a EXCLUSAO desse usuario ou [2] para VOLTAR ao menu de cadastro: ");
        scanf("%i", &cusuario->salvar);

        switch (cusuario->salvar)
        {
            case 1 : break; 
            case 2 : main_delete(); break;
            default:printf("\nOpcao inexistente, tente novamente [1] ou [2]."); 
        }
        
        fclose(puser);
        fclose(temporario); 
        
        remove("dados\\cad_funcionario.txt");
        int rem = 0;
        rem = remove("dados\\cad_funcionario.txt");
        if (rem == 0)
        {
                        rename("dados\\temporario_user_del.txt", "dados\\cad_funcionario.txt");
            printf("O arquivo foi deletado com sucesso!\n");
        }
        else
        {
            remove("dados\\temporario_user_del.txt");
            perror("Nao foi possivel deletar o arquivo!");
            system("pause");
        }

        

        printf("Tecle ENTER para voltar ao menu anterior!");
        getchar();
        main_delete();
    }           
}
  • The error is occurring the second time you remove the file?

  • The error only occurs when trying to remove the remove("data cad_funcionaio.txt"); already removes it("data temporario_user_del.txt"); which is inside Else, is removed normally.

  • Your code is twice remove("dados\\cad_funcionario.txt");.

  • one is the variable receiving the value, but I tested it by leaving only the remove("data cad_funcionaio.txt");, without the rem = remove("data cad_funcionaio.txt"); , but it did not help!

  • It does not remove the file in any way, neither in the first nor the second.

  • The system you use is which ? Windows ? The file already existed beforehand or was created in the code ?

  • Good afternoon colleague, I already managed to solve this problem, I put the fclose out of the while, as soon as the consultation ends, and it worked, vlw by the support.

Show 2 more comments

1 answer

1

as an axiom of programming, I recommend closing the file in the same scope as it was opened, unless you run a "do {} white (...)" there is no certainty that fclose will be called...

and if you are compiled for windows, Rename will not work, instead in windows use:

#include <fileapi.h>
MoveFileA(char*,char*)
ou
MoveFileW(wchar*,wchar*)

although declared, in windows is not functional, as well as other functions that used to enumerate files from a folder, and removes itself (in the case of directories).

*bs.: if you think something is missing in the file... it may be a sign, that you forgot it opened, sometimes the last changes in a file that was not closed are not saved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.