How to create a Folder in c++

closed account (ozUkoG1T)
Hey i am wondering,
How i can create a Raw Folder in c++.

But i do not how to do this so any one here please tell me.
Last edited on
This is 1 way...

1
2
3
4
5
6
    #include <direct.h>
    int main()
    {
          mkdir("c:/myfolder");
          return 0;
    }
Last edited on
There is no standard way to do that.
You have to use OS's specific code.

For example, if you're using windows, you can use the function CreateDirectory() located in windows.h
There is Boost.Filesystem, if you want a cross-platform solution

Filesystem Library
http://www.boost.org/doc/libs/1_51_0/libs/filesystem/doc/index.htm

There's both boost::filesystem::create_directory, which can only create a single directory at a time (like mkdir, or _mkdir for newer versions of VC++, and CreateDirectory/CreateDirectoryEx), and boost::filesystem::create_directories which can handle multiple nested subdirectories, too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <boost/filesystem.hpp>

void test1() {
	const char dir_path[] = "c:\\temp\\cplusplus";

	boost::filesystem::path dir(dir_path);
	if(boost::filesystem::create_directory(dir)) {
		std::cout << "Success" << "\n";
	}
}

void test2() {
	const char dir_path[] = "c:\\temp\\cplusplus\\example\\test";

	boost::filesystem::path dir(dir_path);
	if(boost::filesystem::create_directories(dir)) {
		std::cout << "Success" << "\n";
	}
}

int main() {
	test1();
	test2();

	return 0;
}
Can I use :
system("mkdir \"c:/myfolder\"");
:)
No you can't use:

system("mkdir ...);

mkdir is a UNIX command I thought you were using Windows?

If you are using Windows, I suggest you investigate the CreateDirectory API, see WinBase.h

HTH
No you can't use:

system("mkdir ...);


This is not correct.
This is the same version of the function "CreateProcess" - Command name and parameters...
(I've discovered typing "cmd" will certainly solve this problem. :))
So I can write (Another example) :
system("cmd /c tskill explorer");
Still works correctly. :)
I stand corrected. It's been years since I used MS DOS, I am more used to UNIX command line now. But, it would be more advisable to use the Windows API's.
mkdir is a UNIX command I thought you were using Windows?

The Windows command console supports (ever since the initial version of Windows NT, version 3.1, which begat Windows 2000, Windows XP, etc.) mkdir, too. And the equivalent md command.

Same deal with chdir (cd) and rmdir (rd).

Windows-centric people tend to use the shorter form when writing batch files (do md, cd, rd exist in Linux?).

Andy
Last edited on
Topic archived. No new replies allowed.