View Full Version : how do you make a dir in C/C++

Mr. Book
November 7th, 2007, 04:15 AM
Simple question, I know I must be doing something wrong. I want to make a directory inside my C++ program. I've looked online and found tutorials that say there is a mkdir() function included in <sys/dir.h> (all this does is include dirent.h). This is true and I have gotten it to work under mingw with the code:

if(0<mkdir("doc")) { printf("ERROR\n") return 1; }

But my code does not compile on my ubuntu system. I get the following error:

xcg.cpp:305: error: ‘mkdir’ was not declared in this scope

I did a search inside dirent.h and could not find the string mkdir. searching on google & this forums haven't show what i have been looking for because the "make" and "mkdir" commands overshadow the function that I am looking for. I know this is a simple task and I feel stupid for not being able to find it. any help would be greatly appreciated.

November 7th, 2007, 04:58 AM
You probably found this in google too


Perhaps the directory name is not compatible, causing mkdir() to fail.

November 7th, 2007, 05:05 AM
Try <sys/stat.h>.

Mr. Book
November 7th, 2007, 05:36 AM
Try <sys/stat.h>.

that did the trick. well kinda, i am missing a parameter now, but knowing the include file lead me to this link:


Thank you

November 7th, 2007, 09:51 AM
Also look at the man-page:

$ man 2 mkdir

All the info you need is there.

November 7th, 2007, 07:22 PM
Also check out Boost's filesystem library: