Deleting a Directory using c++ in Linux
I want to delete a directory(which contains files and subfolders) using c++ in Linux, is there any solution to do this one?
any help would be appreciated. |
Re: Deleting a Directory using c++ in Linux
Quote:
Code:
#include <stdlib.h> To really do this in C/C++ your actually need to walk the entire directory tree, deleting all files and then the directory itself. And when there's sub directory inside it, you first need to do the same with that directory. Here's a recursive program that does this. It's in C, but should work in C++. Please be carefull! It is at least as dangerous as "rm -rf" I did not test this thourougly, and I don't take responsibility for any data deleted by it. Test it yourself until confident before including the actual deletion code. Code:
#include <stdio.h> |
Thanks Hko
thanks for the solution with this example i managed to delete the non-empty directory. |
Oh yes,... If you use the code above, please remove this part below. It was added in the process, and doesn't make sense in the code as posted.
Code:
/* obsolete and possibly introduces false errors */ |
Why do you have to go through all subfolders instead of deleting the entire directory at once, i understand there is an option where you delete a directory neverminding what's in it, right?
|
Yes, the "rm" program has an option to delete an entire directory tree. But this is about how the "rm" program does that.
|
Oh i see. You are trying to implement rm. Cool.
|
Thanks
Thank you very much.
I was searching this exact problem my first half problem is solved I need the second half Now I was copy recursivly copy a Directory which have more than one sub directory and each directoy have directory or folders .I was to copy this entir folder to a different location through C and linux platform. my problem is: cd 1. rm -rf * [solved by above answar] 2. cp -r /mnt/usbdrive/finalPackage ~ [waiting to be solved.....] Kindly suggest .. I don't want to use system() Thanks & Regards Dwijesh Maharana |
С++ delete folder recursive
void delete_folder_tree(const char *dirname)
{ DIR *dir; struct dirent *entry; char path[PATH_MAX]; if (path == NULL) { _TRACE("Out of memory error\n"); return; } dir = opendir(dirname); if (dir == NULL) { _ERROR("Error opendir()"); return; } while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) { snprintf(path, (size_t) PATH_MAX, "%s/%s", dirname, entry->d_name); if (entry->d_type == DT_DIR) { delete_folder_tree(path); } else { unlink(path); } } } closedir(dir); rmdir(dirname); _TRACE("(not really) Deleting: %s\n", dirname); return; } |
Quote:
C++ delete directory C++ remove directory . C++ allows usage of standard "C" library which has the needed function: 'man 2 rmdir'. There is also a thing called 'recursion' which helps in your case. |
ira_2011, please don't drag up old threads like this. In addition, when posting code here, you should use [code] tags to preserve indentation.
|
There is now only one standard library function you need to use for this (and yes, it's standard now):
http://www.boost.org/doc/libs/1_47_0...tml#remove_all |
All times are GMT -5. The time now is 01:59 PM. |