Solution for Exercise 23-5

ex2305

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>

int main()
{
    char curdir[255];

    getcwd(curdir,255);
    printf("Current directory is %s\n",curdir);
    mkdir("very_temporary",S_IRWXU);
    puts("New directory created.");
    chdir("very_temporary");
    getcwd(curdir,255);
    printf("Current directory is %s\n",curdir);
    return(0);
}

Output

Current directory is /Users/dang/C Programming FD/code/23
New directory created.
Current directory is /Users/dang/C Programming FD/code/23/very_temporary

Notes

* When compiling for Windows, remove the second argument for the mkdir() function at Line 11.

* This program generates inconsistent results depending on the platform:

* Remember when working with Windows that the backslash is used as a pathname separator. You must specify two of them in a string literal, \\, for the backslash to be properly interpreted.