In ANSI C, does the remove funcion also work with directories? I'm having this ongoing discussion on this matter at another board where I was told it does but when I tried it it didn't work and I just want to make sure. Thanks.
In ANSI C, does the remove funcion also work with directories? I'm having this ongoing discussion on this matter at another board where I was told it does but when I tried it it didn't work and I just want to make sure. Thanks.
I'm told that it specifically works on "files", for whatever definition of a file ANSI C uses. But you shouldn't listen to me. I don't know much about C yet.
It doesn't work for directories, which is why there are functions like rmdir and such.
Quzah.
Hope is the first step on the road to disappointment.
It does work on Linux and Unix-like systems because everything is considered a file on such machines. Windows makes a distinction between files and directories so I guess that is why it didn't work on my computer.
Hmm... I always thought the only thing in Linux that isn't considered a file... is a directory. :/Originally Posted by caduardo21
Sent from my iPad®
If I remember correctly, I believe that everything in Linux is considered a file.
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
from `man remove`:
SYNOPSIS
#include <stdio.h>
int remove(const char *pathname);
DESCRIPTION
remove deletes a name from the filesystem. It calls
unlink for files, and rmdir for directories.
If the removed name was the last link to a file and no
processes have the file open the file is deleted and the
space it was using is made available for reuse.
If the name was the last link to a file but any processes
still have the file open the file will remain in existence
until the last file descriptor referring to it is closed.
If the name referred to a symbolic link the link is
removed.
If the name referred to a socket, fifo or device the name
for it is removed but processes which have the object open
may continue to use it.
In Linux it DOES work for directories.
In windows too BUT if the directory you want to delete contains other files or dirs it doesnt work. I'm guessing it's the same in Linux although I cant test it there
The behaviour you're describing is what rmdir() does.
On my old version of cygwin, remove() can remove empty directories.
[edit] This page seems to suggest that remove can remove empty directories:
From http://wwwwbs.cs.tu-berlin.de/user-t...ing_Files.html [/edit]Function int unlink (const char *filename)
The unlink function deletes the file name filename. If this is a file's sole name, the file itself is also deleted. (Actually, if any process has the file open when this happens, deletion is postponed until all processes have closed the file.)
Function int rmdir (const char *filename)
The rmdir function deletes a directory. The directory must be empty before it can be removed; in other words, it can only contain entries for `.' and `..'.
Function int remove (const char *filename)
This is the ANSI C function to remove a file. It works like unlink for files and like rmdir for directories.
Last edited by dwks; 08-28-2006 at 10:47 AM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
The directory I tried to delete was empty but remove still did not work.
>The directory I tried to delete was empty but remove still did not work.
What were you expecting? If remove is not required to work on directories, it should come as no suprise if it fails when you try to do that.
My best code is written with the delete key.
this should give more info why it failed:Originally Posted by caduardo21
Code:int main( int argc, char *argv[] ) { if (remove("somedir") == -1) { perror("remove failed"); } return ( 0 ) ; }
I'm also trying to make my program delete a certain directory atm, I've tried rmdir and remove, but the problem is that my directory will never be empty upon deleting.
I can think of making a function to enter the directory, search for all files (and sub-dirs) and delete them one by one, but is there NO way this can be done easier? I.e. isnt there a function that deletes dirs regardless whether its empty or not?
BTW, perror; that's a great tip Laserve, thanks!
OK, I've been Googling a bit more and found quite some hints that there isn't a function for deleting a non-empty directory, so I went ahead and created a function for it. I hope it's useful and would like to hear feedback if necessary. It's not standalone btw, but that should be easy enough for you to change/add.
PS Sorry for a bit Off Topic and (therefore) the thread hijacking.Code:#include <stdbool.h> #include <stdio.h> #include <sys/stat.h> #include <windows.h> #include <dirent.h> /****************************** clear_dir function ****************************/ //* Function: clears a dir and its contents //* Parameters: which_dir: directory to delete //* Returns: 0: ok //* 1: removing failed //* bool clear_dir(char which_dir[256]) { DIR *d; struct dirent *dir; char file[256]; struct stat s; d = opendir(which_dir); if (d) { while ((dir = readdir(d)) != NULL) { // exclude directories if( strcmp( dir->d_name, "." ) == 0 || strcmp( dir->d_name, ".." ) == 0) { continue; } sprintf(file,"./%s/%s", which_dir, dir->d_name); printf("*"); //print * for every deleted file if (opendir(file)!=NULL) //if file actually is a dir { clear_dir(file); } else { if (remove(file) == -1) { printf("\n%s\n", file); perror("Remove failed"); return 1; } } } closedir(d); // Deleting directory if (rmdir(which_dir) == -1) { printf("%s\n", which_dir); perror("Remove failed"); return 1; } } return(0); } /******************************************************************************/
Last edited by rkooij; 09-01-2006 at 06:38 AM.
>there isn't a function for deleting a non-empty directory
You could exec a recursive rm.
My best code is written with the delete key.