Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 631715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:59:47+00:00 2026-05-13T19:59:47+00:00

How to delete a non empty directory in C or C++? Is there any

  • 0

How to delete a non empty directory in C or C++? Is there any function? rmdir only deletes empty directory. Please provide a way without using any external library.

Also tell me how to delete a file in C or C++?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:59:47+00:00Added an answer on May 13, 2026 at 7:59 pm

    You want to write a function (a recursive function is easiest, but can easily run out of stack space on deep directories) that will enumerate the children of a directory. If you find a child that is a directory, you recurse on that. Otherwise, you delete the files inside. When you are done, the directory is empty and you can remove it via the syscall.

    To enumerate directories on Unix, you can use opendir(), readdir(), and closedir(). To remove you use rmdir() on an empty directory (i.e. at the end of your function, after deleting the children) and unlink() on a file. Note that on many systems the d_type member in struct dirent is not supported; on these platforms, you will have to use stat() and S_ISDIR(stat.st_mode) to determine if a given path is a directory.

    On Windows, you will use FindFirstFile()/FindNextFile() to enumerate, RemoveDirectory() on empty directories, and DeleteFile() to remove files.

    Here’s an example that might work on Unix (completely untested):

    int remove_directory(const char *path) {
       DIR *d = opendir(path);
       size_t path_len = strlen(path);
       int r = -1;
    
       if (d) {
          struct dirent *p;
    
          r = 0;
          while (!r && (p=readdir(d))) {
              int r2 = -1;
              char *buf;
              size_t len;
    
              /* Skip the names "." and ".." as we don't want to recurse on them. */
              if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
                 continue;
    
              len = path_len + strlen(p->d_name) + 2; 
              buf = malloc(len);
    
              if (buf) {
                 struct stat statbuf;
    
                 snprintf(buf, len, "%s/%s", path, p->d_name);
                 if (!stat(buf, &statbuf)) {
                    if (S_ISDIR(statbuf.st_mode))
                       r2 = remove_directory(buf);
                    else
                       r2 = unlink(buf);
                 }
                 free(buf);
              }
              r = r2;
          }
          closedir(d);
       }
    
       if (!r)
          r = rmdir(path);
    
       return r;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi All I am using a non blocking Socket for sending messages.We were getting
I'm unable to delete a folder in Windows with the following code: SHFILEOPSTRUCT shfo
I've got some code that I've been using successfully for some years to implement
I'm using self-tracking entities in a WCF client-server application. My WCF service returns various
I'm shifting code from an application built in a non-standard custom PHP framework into
In my iPhone app, I have a (non-grouped) UITableView which uses all the bells
Hi I have some issue regarding constructor and destructor. I have list class, which
I'm trying to figure out which files in Python's (Python 2.6/Python 2.7) tcl folder
I use the (query-replace-regexp from to) expression regularly when making large changes in a
I am working on an asp.net web application which uses a fair bit of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.