How to Delete File or Folder in C Language

Asked

Viewed 2,635 times

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.

  • 3

    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.

  • 1

    post your code, will facilitate the location of your problem.

4 answers

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.

  • 1

    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.

  • 1

    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

You are not signed in. Login or sign up in order to post.