1
I’m having trouble removing a C text file. Soon 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 might be going on?
1
I’m having trouble removing a C text file. Soon 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 might be going on?
3
André see if the code below offers some help.
Since I didn’t have the code you used to verify I made one and tested it here.
Remember what Lucas Henrique commented, make sure your user is allowed to do this removal, in the case below I created the file myself and deleted, 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’ve helped.
Browser other questions tagged c filing-cabinet
You are not signed in. Login or sign up in order to post.
What is the value of Errno after the call to remove?
– hugomg
Paste here your code and its return.
– pmargreff
Maybe
perror()
help you understand what happens:if (remove(filename)) perror(filename);
– pmg
Try running the program as an administrator.
– user2692