_chdir, _wchdir
Change the current working directory.
int _chdir( const char *dirname ); int _wchdir( const wchar_t *dirname );
Parameter
- dirname
- Path of new working directory.
Return Value
These functions return a value of 0 if successful. A return value of –1 indicates that the specified path could not be found, in which case errno is set to ENOENT.
Remarks
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 will be 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.
Generic-Text Routine Mapping:
| TCHAR.H routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
|---|---|---|---|
| _tchdir | _chdir | _chdir | _wchdir |
Requirements
| Routine | Required header | Optional headers | Compatibility |
|---|---|---|---|
| _chdir | <direct.h> | <errno.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
| _wchdir | <direct.h> or <wchar.h> | <errno.h> | Win NT, Win 2000, Win XP |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
// crt_chdir.c
/* This program uses the _chdir function to verify
* that a given directory exists.
*/
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
if( _chdir( argv[1] ) )
printf( "Unable to locate the directory: %s\n", argv[1] );
else
system( "dir *.exe");
}
Sample Output
Volume in drive C is CDRIVE
Volume Serial Number is 0E17-1702
Directory of C:\write
04/21/95 01:06p 3,200 ERRATA.WRI
04/21/95 01:06p 2,816 README.WRI
2 File(s) 6,016 bytes
71,432,116 bytes free
See Also
Directory Control Routines | _mkdir | _rmdir | system | Run-Time Routines and .NET Framework Equivalents