Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-29-2004, 06:12 AM
|
#1
|
|
LQ Newbie
Registered: Aug 2004
Posts: 16
Rep:
|
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.
|
|
|
|
10-29-2004, 07:36 AM
|
#2
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
Re: Deleting a Directory using c++ in Linux
Quote:
Originally posted by ArulPrabhuT
I want to delete a directory(which contains files and subfolders) using c++ in Linux, is there any solution to do this one?
|
Of course you could do somthing like:
Code:
#include <stdlib.h>
/* ... */
system("rm -r /your/directory");
But then not your program is doing the deletion: you call the "rm" program from you own program.
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>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
int removedirectoryrecursively(const char *dirname)
{
DIR *dir;
struct dirent *entry;
char path[PATH_MAX];
if (path == NULL) {
fprintf(stderr, "Out of memory error\n");
return 0;
}
dir = opendir(dirname);
if (dir == NULL) {
perror("Error opendir()");
return 0;
}
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) {
removedirectoryrecursively(path);
}
/*
* Here, the actual deletion must be done. Beacuse this is
* quite a dangerous thing to do, and this program is not very
* well tested, we are just printing as if we are deleting.
*/
printf("(not really) Deleting: %s\n", path);
/*
* When you are finished testing this and feel you are ready to do the real
* deleting, use this: remove*STUB*(path);
* (see "man 3 remove")
* Please note that I DONT TAKE RESPONSIBILITY for data you delete with this!
*/
}
}
closedir(dir);
/*
* Now the directory is emtpy, finally delete the directory itself. (Just
* printing here, see above)
*/
printf("(not really) Deleting: %s\n", dirname);
return 1;
}
int main(int argc, char *argv[])
{
char answer[10];
int yes, no;
if (argc != 2) {
fprintf(stderr, "Usage:\t%s <directory>\n", *argv);
return 1;
}
do {
printf("\nAre you sure you want to delete the entire directory %s?\n",
argv[1]);
fgets(answer, 5, stdin);
yes = !strncmp(answer, "yes\n", 4);
no = !strncmp(answer, "no\n", 3);
if (no) {
printf("\nexit...\n");
return 0;
} else if (yes) {
/*
* Start deleting the directory...
*/
return !removedirectoryrecursively(argv[1]);
} else {
printf("\nPlease answer exactly yes or no.");
}
} while (!yes && !no);
return 0;
}
|
|
|
|
11-01-2004, 08:00 AM
|
#3
|
|
LQ Newbie
Registered: Aug 2004
Posts: 16
Original Poster
Rep:
|
Thanks Hko
thanks for the solution with this example i managed to delete the non-empty directory.
|
|
|
|
11-01-2004, 10:44 AM
|
#4
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
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 */
if (path == NULL) {
fprintf(stderr, "Out of memory error\n");
return 0;
}
|
|
|
|
11-03-2004, 01:43 PM
|
#5
|
|
Member
Registered: Oct 2004
Location: Costa Rica
Distribution: Mandrake 9.1
Posts: 30
Rep:
|
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?
|
|
|
|
11-03-2004, 02:55 PM
|
#6
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
Yes, the "rm" program has an option to delete an entire directory tree. But this is about how the "rm" program does that.
|
|
|
|
11-04-2004, 02:03 AM
|
#7
|
|
Member
Registered: Oct 2004
Location: Costa Rica
Distribution: Mandrake 9.1
Posts: 30
Rep:
|
Oh i see. You are trying to implement rm. Cool.
|
|
|
|
04-30-2009, 04:55 AM
|
#8
|
|
LQ Newbie
Registered: Apr 2009
Posts: 8
Rep:
|
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
|
|
|
|
08-16-2011, 07:23 AM
|
#9
|
|
LQ Newbie
Registered: Aug 2011
Posts: 1
Rep: 
|
С++ 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;
}
|
|
|
|
08-16-2011, 09:01 AM
|
#10
|
|
Senior Member
Registered: May 2005
Posts: 4,392
|
Quote:
Originally Posted by ArulPrabhuT
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.
|
Yes, there is:
- you open your favorite browser;
- you navigate to your favorite web search engine;
- you try search terms like
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.
|
|
|
|
08-20-2011, 02:14 AM
|
#11
|
|
LQ Addict
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,466
Rep: 
|
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.
|
|
|
|
08-20-2011, 11:10 AM
|
#12
|
|
Senior Member
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 3,659
|
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
Last edited by dugan; 08-20-2011 at 11:12 AM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:22 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|