How to delete a file or folder by code in c language, I already tried to use the remove(pt);
method but it did not work, I tried using DOS commands and it did not work either.
How to delete a file or folder by code in c language, I already tried to use the remove(pt);
method but it did not work, I tried using DOS commands and it did not work either.
In 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;
}
See the manual for remove
.
Note: In C we call this function, not method.
Basic example with a simple test to see if it has been deleted or not.
int retorno;
char arquivo[] = "arquivo.txt";
retorno = remove(arquivo);
if(retorno == 0) {
printf("deletado");
} else {
printf("nao deletado");
}
The function is called remove()
, declared in <stdio.h>
. Read about it in your favorite reference (as you seem to use Windows, it will probably be the MSDN).
To delete a file, we use the remove("nome_do_arquivo")
function. 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 have helped.
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());
}