### chdir -- Change Current Working Directory

Format

>     #include <direct.h>
>     int chdir(char *pathname);

Language Level: XPG4, Extension
chdir causes the current working directory to change to the directory specified by pathname. The pathname must refer to an existing directory.

Note: This function can change the current working directory on any drive. It cannot change the default drive. For example, if A:\BIN is the current working directory and A: is the default drive, the following changes only the current working directory on drive C:.

       chdir ("c:\\emp");

A: is still the default drive.

An alternative to this function, on OS/2, is the DosSetCurrentDir API call.

On Windows, an alternative to this function is the SetCurrentDir API call.

Note: In earlier releases of the C/C++ run-time library, chdir began with an underscore (_chdir). Because it is defined by the X/Open standard, the underscore has been removed. For compatibility, IBM C and C++ Compilers will map _chdir to chdir for you.

Return Value
chdir returns a value of 0 if the working directory was successfully changed. A return value of -1 indicates an error; chdir sets errno to ENOENT, showing that chdir cannot find the specified path name. No error occurs if pathname specifies the current working directory.

Example
This example changes the current working directory to the root directory, and then to the \red\green\blue directory.

    #include <direct.h>
    #include <stdio.h>

    int main(void)
    {
       printf("Changing to the root directory.\n");
       if (chdir("\\"))
          perror(NULL);
       else
          printf("Changed to the root directory.\n\n");
       printf("Changing to directory '\\red\\green\\blue'.\n");
       if (chdir("\\red\\green\\blue"))
          perror(NULL);
       else
          printf("Changed to directory '\\red\\green\\blue'.\n");
       return 0;

       /**************************************************************
          If directory \red\green\blue exists, the output should be:

          Changing to the root directory.
          Changed to the root directory.

          Changing to directory '\red\green\blue'.
          Changed to directory '\red\green\blue'.
       **************************************************************/
    }

_chdrive -- Change Current Working Drive
_getcwd -- Get Path Name of Current Directory
_getdcwd -- Get Full Path Name of Current Directory
_getdrive -- Get Current Working Drive
system -- Invoke the Command Processor
mkdir -- Create New Directory
rmdir -- Remove Directory
<direct.h>