Changes the current working directory.
#include <direct.h> | Required only for function declarations | |
#include <errno.h> | Required for errno constants |
int _chdir( char *dirname );
dirname | Path name of new working directory |
The _chdir function changes the current working directory to the directory specified by dirname. The dirname argument must refer to an existing directory.
This function can change the current working directory on any drive; it cannot be used to change the default drive itself. For example, if A: is the default drive and \BIN is the current working directory, the following call changes the current working directory for drive C:
_chdir("c:\\temp");
Notice that you must place two backslashes (\\) in a C string in order to represent a single backslash (\); the backslash is the escape character for C strings and therefore requires special handling.
This function call has no apparent immediate effect. However, when the _chdrive function is called to change the default drive to C:, the current working directory becomes C:\TEMP.
With DOS, the new directory set by the program becomes the new current working directory.
The _chdir function returns a value of 0 if the working directory is successfully changed. A return value of –1 indicates an error, in which case errno is set to ENOENT, indicating that the specified path name could not be found.
Standards:UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
Use _chdir for compatibility with ANSI naming conventions of non-ANSI functions. Use chdir and link with OLDNAMES.LIB for UNIX compatibility.
_dos_setdrive, _mkdir, _rmdir, system
/* CHGDIR.C: This program uses the _chdir function to verify that a
* given directory exists. Under real mode that directory also becomes
* the current directory. Under protected mode, it is only the default
* directory for the current process.
*/
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
void main( int argc, char *argv[] )
{
if( _chdir( argv[1] ) )
printf( "Unable to locate the directory: %s\n", argv[1] );
else
system( "dir *.c" );
}
[C:\LIBREF] chgdir \tmp
The volume label in drive C is ZEPPELIN.
Directory of C:\TMP
DUP C 232 4-18-99 11:18a
TEST C 713 4-07-98 2:49p
2 File(s) 14155776 bytes free