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

  • SEARCH
  • Home
  • 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

Is there a way to delete all files & sub-directories of a specified directory
How do I delete a possibly non-empty dir in Python. The directory may have
I want to delete all but the 4 newest directories in my parent directory.
I'd like to delete a directory that may or may not contain files or
I'm using MVC ASP.NET 3.5 and I'm trying to delete a folder with all
I opened up regedit and made an empty binary value monitoring it using ProcessMonitor.exe.
I want to delete foo() if foo() isn't called from anywhere.
I erroneously delete all the rows from a MS SQL 2000 table that is
How do you delete a cookie in rails that was set with a wild
I had to delete all the rows from a log table that contained about

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.