Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to create a folder using c++ programming?
Posted
Updated 22-Feb-11 23:07pm
v2
Comments
Henry Minute 17-Feb-11 19:58pm    
Managed or unmanaged?

It works for me without using API and system() function!
C
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <dir.h>
#define DIRNAME "c:\\testdir"
int main(void)
{
   int stat;
   stat = mkdir(DIRNAME);
   if (!stat)
      printf("Directory created\n");
   else
   {
      printf("Unable to create directory\n");
      exit(1);
   }
   getch();
   system("dir/p");
   getch();
   stat = rmdir(DIRNAME);
    if (!stat)
      printf("\nDirectory deleted\n");
   else
   {
      perror("\nUnable to delete directory\n");
      exit(1);
   }
   return 0;
}
 
Share this answer
 
CreateDirectory is the Windows API call that creates a directory.

Directory::CreateDirectory would be the .NET equivalent (assuming you are using C++/CLI).

I don't believe Standard C++ has the concept of directories since that is OS dependent.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Feb-11 22:27pm    
Hm. Teamwork - a 5.
--SA
anjansarma 15-Apr-13 11:47am    
What shall I have to do to create a directory in a drive other than C drive ?
This code creates a directory in C drive only. And what to do to display an error message if there already exits a directory with the same name provided as input?
C++ does not API to create directories. If you are using Windows/Managed C++, follow Nishant answer. For standard C++ you can use the C run-time _tmkdir[^]

_tmkdir("\\testtmp")
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Feb-11 22:27pm    
Hm. Teamwork - a 5.
--SA
You can also use this

System("mkdir C:\\NewDir");
 
Share this answer
 
Comments
Member 14608292 22-Oct-19 2:45am    
how to check whether the file is created or not
If you have access to Boost Filesystem library[^], there is a function create_directory[^] for that.
 
Share this answer
 
You can use CreateDirectory API. Pass the first parameter as the foldername.
 
Share this answer
 
An alternative: SHCreateDirectory
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900