Question about the remove function

This is a discussion on Question about the remove function within the C Programming forums, part of the General Programming Boards category; In ANSI C, does the remove funcion also work with directories? I'm having this ongoing discussion on this matter at ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Question about the remove function

    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.

  2. #2
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    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.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    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.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Quote Originally Posted by caduardo21
    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. :/
    Sent from my iPad®

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    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

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    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

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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:
    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.
    From http://wwwwbs.cs.tu-berlin.de/user-t...ing_Files.html [/edit]
    Last edited by dwks; 08-28-2006 at 11: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.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    The directory I tried to delete was empty but remove still did not work.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by caduardo21
    The directory I tried to delete was empty but remove still did not work.
    this should give more info why it failed:

    Code:
    int main( int argc, char *argv[] )
    {
        if (remove("somedir") == -1)
        {
            perror("remove failed");
        }
    
        return ( 0 ) ;
    }

  13. #13
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    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!
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  14. #14
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    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.

    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);
    }
    /******************************************************************************/
    PS Sorry for a bit Off Topic and (therefore) the thread hijacking.
    Last edited by rkooij; 09-01-2006 at 07:38 AM.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21