Just A Simple MKDIR code please.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • papayrus
    New Member
    • May 2010
    • 2

    Just A Simple MKDIR code please.

    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

    Code:
    #include <direct.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int main( void )
    {
       if( _mkdir( "Directory" ) == 0 )
          printf( "Directory '\\testtmp' was successfully created\n" );
          system( "dir \\testtmp" );
    
    }
    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.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    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.
    etc..

    Comment

    • papayrus
      New Member
      • May 2010
      • 2

      #3
      Hello

      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 .

      Code:
      /* MAKEDIR.C */
      
      #include <direct.h>
      #include <stdlib.h>
      #include <stdio.h>
      
      void main( void )
      {
         if( _mkdir( "\\testtmp" ) == 0 )
         {
            printf( "Directory '\\testtmp' was successfully created\n" );
            system( "dir \\testtmp" );
            if( _rmdir( "\\testtmp" ) == 0 )
              printf( "Directory '\\testtmp' was successfully removed\n"  );
            else
               printf( "Problem removing directory '\\testtmp'\n" );
         }
         else
            printf( "Problem creating directory '\\testtmp'\n" );
      }
      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.

      Comment

      Working...