How to delete file or folder in C Language

4

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.

    
asked by anonymous 17.02.2017 / 20:56

4 answers

2

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.

    
18.05.2018 / 11:22
0

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");
   }
    
18.05.2018 / 12:16
0

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.

    
19.02.2017 / 23:12
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());
}
    
04.10.2018 / 19:00