By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,617 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,617 IT Pros & Developers. It's quick & easy.

Just A Simple MKDIR code please.

P: 2
I been trying to simply make a directory in C++ but I dont get what all these people are talking about and they never put a complete code in there I can use that includes the includes ect. Can you give me a code that will do this simple and straight forward that I can just copy and paste and it will work where all I need to do is change the directory name to what I want it to be please?
Regards and thanks. By the way I did try this and it worked

Expand|Select|Wrap|Line Numbers
  1. #include <direct.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  5. int main( void )
  6. {
  7.    if( _mkdir( "Directory" ) == 0 )
  8.       printf( "Directory '\\testtmp' was successfully created\n" );
  9.       system( "dir \\testtmp" );
  11. }
BUT this doesnt seem like its the best way to simply make a directory even though it works. I dont think there should be an if statement needed for one. I just want to break it down to what is actually needed and I need the whole code including includes. Also I am confused to why I need the printf and the system dir testtemp lines.
May 3 '10 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 2,057
Have you tried removing the lines to see if the program still works?

If you succeed in creating your directory what do you want to happen?
What if you fail? Have you considered the error possibilities?

1. File already exists with that name.
2. Permissions errors.
3. Directory already exists.
May 3 '10 #2

P: 2
Thanks for the fast response. Well actually I have taken alot of the lines out of the main code already and it seems if I take anything else out I get errors. The main code came from this .

Expand|Select|Wrap|Line Numbers
  1. /* MAKEDIR.C */
  3. #include <direct.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  7. void main( void )
  8. {
  9.    if( _mkdir( "\\testtmp" ) == 0 )
  10.    {
  11.       printf( "Directory '\\testtmp' was successfully created\n" );
  12.       system( "dir \\testtmp" );
  13.       if( _rmdir( "\\testtmp" ) == 0 )
  14.         printf( "Directory '\\testtmp' was successfully removed\n"  );
  15.       else
  16.          printf( "Problem removing directory '\\testtmp'\n" );
  17.    }
  18.    else
  19.       printf( "Problem creating directory '\\testtmp'\n" );
  20. }
I changed void to int which worked and I deleted lines until I got the original code I posted but it seems like there should be a better way like a more direct way less lines of code. Also I dont understand what the line that says ( "dir \\testtmp" ); is for. Thanks Again Appeciate it.
May 3 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.