krystian71115
krystian71115

Reputation: 1937

How to force deletion of file in C?

How can I remove opened file in linux?

In shell I can do this:

rm -rf /path/to/file_or_directory

But how can I do it in C?

I don't want to using system() function.

I have seen unlink and remove method but it's haven't any flags to set force deletion.

Upvotes: 3

Views: 6992

Answers (4)

Kaz
Kaz

Reputation: 58627

To perform a recursive removal, you have to write a moderately complicated program which performs a file system walk. ISO C has no library features for this; it requires platform-specific functions for scanning the directory structure recursively.

On POSIX systems you can use opendir, readdir and closedir to walk individual directories, and use programming language recursion to handle subdirectories. The functions ftw and its newer variant nwft perform an encapsulated file system walk; you just supply a callback function to process the visited paths. nftw is better because it has a flags argument using which you can specify the FTW_DEPTH flag to do the search depth first: visit the contents of a directory before reporting the directory. That, of course, is what you want for recursive deletion.

On MS Windows, there is FindFirstFile and FindNextFile to cob together a recursive traversal.

About -f, that only suppresses certain checks done by the rm program above and beyond what the operating system requires. Without -f, you get prompted if you want to delete a read-only file, but actually, in a Unix-like system, only the directory write permission is relevant, not that of the file, for deletion. The remove library function doesn't have such a check.

By the way, remove is in ISO C, so it is platform-independent. On POSIX systems, it calls rmdir for directories and unlink for other objects. So remove is not only portable, but lets you not worry about what type of thing you're deleting. If a directory is being removed, it has to be empty though. (Not a requirement of the remove function itself, but of mainstream operating systems that support it).

Upvotes: 1

LearningCODE
LearningCODE

Reputation: 165

Well, I hope this answers your question.. This program searches the current directory for the filename, you have to add the feature of opening a different directory, which shouldn't be too hard... I don't understand the last line of your question, can you elaborate? But flags aren't necessary for remove and unlink (They force delete)...

#include<stdio.h>

int main()
{
   int status;
   char file_name[25];

   printf("Enter the name of file you wish to delete\n");
   fgets(file_name,25,stdin);

   status = remove(file_name);

   if( status == 0 )
      printf("%s file deleted successfully.\n",file_name);
   else
   {
      printf("Unable to delete the file\n");
      perror("Error");
   }

   return 0;
}

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490623

remove or unlink is basically equivalent to rm -f already--that is, it removes the specified item without prompting for further input.

If you want something equivalent to rm -r, you'll need to code up walking through the directory structure and deleting items individually. Boost Filesystem (for one example) has code to let you do that fairly simply while keeping the code reasonably portable.

Upvotes: 0

Barmar
Barmar

Reputation: 782285

The unlink and remove functions force deletion. The rm command is doing extra checks before it calls one of those functions. But once you answer y, it just uses that function to do the real work.

Upvotes: 7

Related Questions