Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing files from C code

Tags:

c

I have to remove few hundreds of files inside my C code. I use "remove" in a loop. Is there any faster way to do it than using "remove"? I ask this because I can't give wildchars using "remove".

like image 701
Kitcha Avatar asked Nov 18 '11 02:11

Kitcha


People also ask

Can I delete files from C?

Right-click your main hard drive (usually the C: drive) and select Properties. Click the Disk Cleanup button and you'll see a list of items that can be removed, including temporary files and more. For even more options, click Clean up system files. Tick the categories you want to remove, then click OK > Delete Files.

What is remove () in C?

The remove function in C/C++ can be used to delete a file. The function returns 0 if files is deleted successfully, other returns a non-zero value. #include<stdio.h> int main() {

How do I delete an existing file in C++?

The remove() function takes the following parameter: filename - pointer to the C-string containing the name of the file along with the path to delete.


1 Answers

No, there isn't a quicker way than using remove() - or unlink() on POSIX systems - in a loop.

The system rm command does that too - at least in the simple, non-recursive case where the names are given on the command line. The shell expands the metacharacters, and rm (in)famously goes along deleting what it was told to delete, unaware of the disastrous *.* notation that was used on the command line. (In the recursive case, it uses a function such as nftw() to traverse the directory structure in depth-first order and repeated calls to unlink() to remove the files and rmdir() to remove the (now-empty) directories.)

POSIX does provide functions (glob() and wordexp()) to generate lists of file names from metacharacters as used in the (POSIX) shell, plus fnmatch() to see whether a name matches a pattern.

like image 166
Jonathan Leffler Avatar answered Nov 12 '22 03:11

Jonathan Leffler