Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating Directory

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Am new to C. Is there a wat to create a directory recursively in C???? say C://test1/project/message
Thanx.
 
There is an mkdir() API in UNIX machines.

NAME
mkdir - make a directory

SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>

int mkdir (path, mode)
char *path;
mode_t mode;

Use mkdir() to make a directory then enter that directory by invoking the chdir() API, everything else is the beauty of recursion.

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
Code:
#include <windows.h>

DWORD CreateDir ( char *pb )

// Creates a directory, whose name is stored in pb
// Creates all underlying directories, too.
// return value : 0 if successfull, a win32
// error code if not successfull.
{
  if ( CreateDirectory ( pb, NULL )) return 0;

  DWORD Erc = GetLastError ( );
  if ( Erc == 183 ) return 0;    // Already exists --> OK

  if ( Erc != 3 ) return Erc;    // Path does not exist

  char SVDir[MAX_PATH];
  strcpy ( SVDir, pb );

  BOOL  fOK   = FALSE;
  DWORD i1;
  DWORD Depth = 0;

  do { for ( i1 = strlen ( SVDir ) - 1;
             i1 > 0 && !fOK && SVDir [i1] != '\\';
             i1-- );
       if ( i1 == 0 ) return Erc;
          { SVDir[i1] = '\0';
            if ( CreateDirectory ( SVDir, NULL )) fOK = TRUE; else
               { Erc = GetLastError ( );
                 if ( Erc == 183 ) fOK = TRUE; else
                    { Depth++;
                      if ( Erc != 3 ) return Erc; } }
          } } while ( i1 > 0 && !fOK );

  for ( i1 = Depth; i1 > 0; i1-- )
     { SVDir [ strlen ( SVDir )] = '\\';
       if ( !CreateDirectory ( SVDir, NULL ))
          { Erc = GetLastError ( );
            if ( Erc != 3 ) return Erc; } }

  if ( CreateDirectory ( pb, NULL )) return 0;
  Erc = GetLastError ( );
  return Erc; }
Marcel
 
Sorry, the previous code still had some C++ features in it. Here is plain C:

Code:
#include <windows.h>

DWORD CreateDir ( char *pb )

// Creates a directory, whose name is stored in pb
// Creates all underlying directories, too.
// return value : 0 if successfull, a win32
// error code if not successfull.
{
  BOOL  fOK;
  DWORD Erc;
  DWORD Depth;
  DWORD i1;
  char  SVDir[MAX_PATH];

  if ( CreateDirectory ( pb, NULL )) return 0;

  Erc = GetLastError ( );
  if ( Erc == 183 ) return 0;    // Already exists --> OK

  if ( Erc != 3 ) return Erc;    // Path does not exist

  strcpy ( SVDir, pb );

  fOK   = FALSE;
  Depth = 0;

  do { for ( i1 = strlen ( SVDir ) - 1;
             i1 > 0 && !fOK && SVDir [i1] != '\\';
             i1-- );
       if ( i1 == 0 ) return Erc;
          { SVDir[i1] = '\0';
            if ( CreateDirectory ( SVDir, NULL )) fOK = TRUE; else
               { Erc = GetLastError ( );
                 if ( Erc == 183 ) fOK = TRUE; else
                    { Depth++;
                      if ( Erc != 3 ) return Erc; } }
          } } while ( i1 > 0 && !fOK );

  for ( i1 = Depth; i1 > 0; i1-- )
     { SVDir [ strlen ( SVDir )] = '\\';
       if ( !CreateDirectory ( SVDir, NULL ))
          { Erc = GetLastError ( );
            if ( Erc != 3 ) return Erc; } }

  if ( CreateDirectory ( pb, NULL )) return 0;
  Erc = GetLastError ( );
  return Erc; }


void main ( )
{ CreateDir ( &quot;C:\\A\\B\\C\\D\\E\\F&quot; ); }

Marcel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top