Remove text file

0

I'm having trouble removing a text file in C. Shortly after using the fclose () function, I use remove () indicating the name of the text file, but the file is not deleted (the remove () function is not returning 0); What could be happening?

    
asked by anonymous 26.04.2015 / 03:27

1 answer

1

André, see if the code below offers some help.

As I did not have the code you used to check I did one and tested it here.

It's worth remembering what Lucas Henrique commented, make sure that your user is allowed to do this removal, in the case below I created the file and erased it, if you are just reading an existing file maybe your user is not allowed to remove the file.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // inclui apenas para usar o sleep

int main(void) {
    FILE *fp;
    char * file_name;

    file_name = "testeremove";

    printf("Criando arquivo\n");

    fp = fopen(file_name, "w");
    if (fp == NULL)
    {
        printf("erro ao criar o arquivo para escrita\n");
    }
    else
    {
        printf("Colocando um conteudo no arquivo\n");

        fprintf(fp, "Colocando um conteudo qualquer");
        fclose(fp);

        sleep(10); // aguarda 10 segundos antes de apagar o arquivo, coloquei para você poder checar o arquivo criado
        int ret;
        ret = remove(file_name);
        printf("%d\n",ret);
    }

    return EXIT_SUCCESS;
}

I hope I have helped.

    
13.05.2015 / 20:50