Name
chdir - Changes current working directory.
Syntax
# include <direct.h>
int chdir(pathname)
char *pathname;
Description
The chdir function changes the current working directory to
the directory specified by pathname. The pathname 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 A:\BIN
is the current working directory, the following call changes
the current working directory for the drive C:
chdir("c:\\temp");
Notice that you must place two backslashes (\\) in a string
in order to represent a single backslash (\); the backslash
is the escape character for strings and therefore requires
special handling. In the previous example, call the system
function to change the current default drive to C: before
changing the working directory to that drive.
This function call has no apparent immediate effect.
However, when the OS/2 function DosSelectDisk is called to
change the default drive to C:, the current working
directory becomes C:\TEMP.
Notes
In OS/2 protected mode, the current working directory is
local to a process rather than system wide.
Return Value
The chdir function returns a value of 0 if the working
directory is successfully changed. A return value of -1
indicates an error and errno is set to ENOENT indicating
that the specified path name could not be found. No error
occurs if pathname specifies the current working directory.
See Also
mkdir(DOS), rmdir(DOS), system(DOS)
Example
#include <direct.h> #include <stdio.h>
main(argc, argv) int argc; char *argv[];
{
int rtnval;
if (rtnval = chdir(argv[1]))
printf("Problem occurred changing to
directory %s",
argv[1]);
else
printf("Change to directory %s was
successful",
argv[1]);
}
This program uses the chdir function to emulate the DOS cd
command.
(printed 6/18/89)