4
How to delete a file or folder by code in c language, I’ve tried using the method remove(pt);
but it didn’t work, I tried using dos commands and it didn’t work either.
4
How to delete a file or folder by code in c language, I’ve tried using the method remove(pt);
but it didn’t work, I tried using dos commands and it didn’t work either.
2
On Linux you need to use the function remove
.
#include <stdio.h>
int main()
{
remove("caminho completo do arquivo ex. /home/user/arquivo.txt");
return 0;
}
Refer to the function manual remove
.
NOTE: In C we call this function, not method.
This is the canonical form in C, whether on Linux or any operating system.
0
basic example with a simple test to see if it was deleted or not.
int retorno;
char arquivo[] = "arquivo.txt";
retorno = remove(arquivo);
if(retorno == 0) {
printf("deletado");
} else {
printf("nao deletado");
}
0
The function is called remove()
, stated in <stdio.h>
. Read about it in your favorite reference (as you seem to use Windows, it will probably be MSDN).
To delete a file, we use the function remove("nome_do_arquivo")
. In the example below the program will delete the file stack.txt
:
#include <stdio.h>
int main()
{
remove("stack.txt");
return 0;
}
I hope I’ve helped.
After analyzing the whole situation, I left as the Edit approved by @Higor, even with some improvements. The only problem is that now the answer is the same as the other two posted before the correction
0
Example of how to delete file and folder in windows using Windows. h.
void deletar_arquivo (string caminho) {
DeleteFileA(caminho.c_str());
}
void deletar_pasta (string pasta) {
RemoveDirectoryA(pasta.c_str());
}
Browser other questions tagged c
You are not signed in. Login or sign up in order to post.
The basic command is this, if it did not work there is something wrong with your code. Post it and indicate what is wrong so we can help.
– Maniero
post your code, will facilitate the location of your problem.
– HudsonPH