Solution for Exercise 23-6
ex2306 - Unix Version
#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);
if(mkdir("very_temporary",755) == -1)
{
puts("Directory creation error");
return(1);
}
puts("New directory created.");
if(chdir("very_temporary") == -1)
{
puts("Unable to change directories");
return(1);
}
getcwd(curdir,255);
printf("Current directory is %s\n",curdir);
return(0);
}
ex2306 - Windows Version
#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);
if(mkdir("very_temporary") == -1)
{
puts("Directory creation error");
return(1);
}
puts("New directory created.");
if(chdir("very_temporary") == -1)
{
puts("Unable to change directories");
return(1);
}
getcwd(curdir,255);
printf("Current directory is %s\n",curdir);
return(0);
}
Notes
* The difference between the versions is found at Line 12.
* The chdir() and mkdir() functions return 0 upon success, -1 otherwise.
Copyright © 1997-2025 by QPBC.
All rights reserved
