Syntax
#include <direct.h> int chdir(char *pathname);Description
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 is the DosSetCurrentDir call.
Returns
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.
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'. ****************************************************************************/ }
Related Information