_chdir, _wchdir
Changes the current working directory.
int _chdir( const char *dirname ); int _wchdir( const wchar_t *dirname );
These functions return a value of 0 if successful. A return value of –1 indicates failure. If the specified path could not be found, errno is set to ENOENT. If dirname is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, errno is set to EINVAL and the function returns -1.
The _chdir function changes the current working directory to the directory specified by dirname. The dirname parameter must refer to an existing directory. This function can change the current working directory on any drive. If a new drive letter is specified in dirname, the default drive letter is changed as well. For example, if A is the default drive letter and \BIN is the current working directory, the following call changes the current working directory for drive C and establishes C as the new default drive:
_chdir("c:\\temp");
When you use the optional backslash character (\) in paths, you must place two backslashes (\\) in a C string literal to represent a single backslash (\).
_wchdir is a wide-character version of _chdir; the dirname argument to _wchdir is a wide-character string. _wchdir and _chdir behave identically otherwise.
Tchar.h routine | _UNICODE and _MBCS not defined | _MBCS defined | _UNICODE defined |
|---|---|---|---|
_tchdir | _chdir | _chdir | _wchdir |
Routine | Required header | Optional header |
|---|---|---|
_chdir | <direct.h> | <errno.h> |
_wchdir | <direct.h> or <wchar.h> | <errno.h> |
For more compatibility information, see Compatibility in the Introduction.
// crt_chdir.c
// arguments: C:\WINDOWS
/* This program uses the _chdir function to verify
that a given directory exists. */
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main( int argc, char *argv[] )
{
if(_chdir( argv[1] ) )
{
switch (errno)
{
case ENOENT:
printf( "Unable to locate the directory: %s\n", argv[1] );
break;
case EINVAL:
printf( "Invalid buffer.\n");
break;
default:
printf( "Unknown error.\n");
}
}
else
system( "dir *.exe");
}