Working with directories Part IICreating and removing directories!
First off, if you haven't already, I suggest you read
Part I of this tutorial, since a function which I have written will utilise everything that is taught in that tutorial.
Next, take a look at
this snippet which I wrote. It should make sense, if you understood Part I.
Good, now we're ready to start. I will be making use of that snippet in this tutorial, to show our progress.
Let's begin!What are we going to cover in this tutorial?Well, in Part I, we learned how to access a directory, using DIR* and dirent*
Now, in this tutorial, we will learn how to use some (simple) functions, which perform the following tasks:
- Creating a new directory
- Deleting a directory
- Changing our active directory
- Getting the whole path of the current directory
As with Part I, this is compatible with both C++ and C, therefore I will provide the main code in C, and convert it to C++ at the end. (Again, the only difference is console output)
First off, I'm going to give you the bulk of the code. This is the includes and the function which we will use to list the contents of the current directory:
cpp
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
void listdir (const char *path);
int main ()
{
// example code will go here
return 0;
}
void listdir (const char *path)
{
// first off, we need to create a pointer to a directory
DIR *pdir = NULL; // remember, it's good practice to initialise a pointer to NULL!
pdir = opendir (path); // "." will refer to the current directory
struct dirent *pent = NULL;
if (pdir == NULL) // if pdir wasn't initialised correctly
{ // print an error message and exit the program
printf ("\nERROR! pdir could not be initialised correctly");
return; // exit the function
} // end if
while (pent = readdir (pdir)) // while there is still something in the directory to list
{
if (pent == NULL) // if pent has not been initialised correctly
{ // print an error message, and exit the program
printf ("\nERROR! pent could not be initialised correctly");
return; // exit the function
}
// otherwise, it was initialised correctly. let's print it on the console:
printf ("%s\n", pent->d_name);
}
// finally, let's close the directory
closedir (pdir);
}
Creating a new directoryWe can do this with a call to a simple function, call
mkdir() like so:
cpp
mkdir (".\\dream.in.code");The
. represents our current directory (by default, this is the directory from which the program was launched)
So, now we have created a new folder called "dream.in.code"
Getting the current directoryThis can be done with a call to a function called
getcwd() like so:
cpp
printf ("Currently viewing:\n%s\n", getcwd(NULL,0));As you can see, we are outputting the return value of the function, which is a string referring to the path of the current directory. Note that the function accepts two parameters. We aren't going to go into detail right now, since this is just the basics of directory access, but just remember that you can pass NULL,0 to it to get the current path

Just to clarify that our dream.in.code folder has been created successfully, let's list the contents of our current directory, using my snippet:
cpp
listdir (".");Remember,
. refers to the current path. Now, if you run the program, you should see a folder called dream.in.code at the bottom of the list. Now let's move into that folder!
Changing the active directoryAgain, all that we need to do is make a call to a simple function called
chdir() I'm going to do this, and call the
getcwd() and
listdir() functions for the output, to keep output clear.
cpp
chdir (".\\dream.in.code");
printf ("\nCurrently viewing:\n%s\n", getcwd(NULL,0));
listdir (".");NOTE: At this point, the output from this listdir() call should just be:
.
..because
. refers to the current directory, and
.. refers to the parent directory. There will be nothing else, since we have not put anything into this folder.
Now, just for clarity, I'm going to move back a folder, using
.. and output the current path, like so:
cpp
chdir ("..\\"); // move back one
printf ("Currently viewing:\n%s\n", getcwd(NULL,0));Finally, let's remove our "dream.in.code" directory.
Removing a directoryYet another single function call! Isn't this directory stuff easy? Now, to remove a directory, we can make a call to
rmdir() like so:
cpp
rmdir (".\dream.in.code");And finally, I'll make another call to my snippet in order to reveal on the console that our directory has gone:
cpp
listdir (".");And that's all there is to it!Why not go down to the local bar and try to pick up some girls with your super cool new directory accessing skills? I'm sure they'll be impressed!

Now for the final code again!
C CODE!cpp
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
void listdir (const char *path);
int main ()
{
if (!mkdir (".\\dream.in.code"))
{
printf ("mkdir() unsuccessful. Terminating...");
exit (3); // exit after a 3 second pause
} // otherwise, it was created, so we can continue
printf ("dream.in.code folder created successfully!");
printf ("Currently viewing:\n%s\n", getcwd(NULL,0));
listdir (".");
printf ("\nMoving into our newly created directory");
chdir (".\\dream.in.code");
printf ("\nCurrently viewing:\n%s\n", getcwd(NULL,0));
listdir (".");
chdir ("..\\"); // move back one
rmdir (".\dream.in.code");
listdir (".");
return 0;
}
void listdir (const char *path)
{
// first off, we need to create a pointer to a directory
DIR *pdir = NULL; // remember, it's good practice to initialise a pointer to NULL!
pdir = opendir (path); // "." will refer to the current directory
struct dirent *pent = NULL;
if (pdir == NULL) // if pdir wasn't initialised correctly
{ // print an error message and exit the program
printf ("\nERROR! pdir could not be initialised correctly");
return; // exit the function
} // end if
while (pent = readdir (pdir)) // while there is still something in the directory to list
{
if (pent == NULL) // if pent has not been initialised correctly
{ // print an error message, and exit the program
printf ("\nERROR! pent could not be initialised correctly");
return; // exit the function
}
// otherwise, it was initialised correctly. let's print it on the console:
printf ("%s\n", pent->d_name);
}
// finally, let's close the directory
closedir (pdir);
}
C++ CODE!cpp
#include <dirent.h>
#include <cstdio> // for the snippet
#include <iostream> // for output
void listdir (const char *path);
int main ()
{
if (!mkdir (".\\dream.in.code"))
{
printf ("mkdir() unsuccessful. Terminating...");
exit (3); // exit after a 3 second pause
} // otherwise, it was created, so we can continue
std::cout << "dream.in.code folder created successfully!"
<< "Currently viewing:\n%s\n" << getcwd(NULL,0);
listdir (".");
std::cout << "\nMoving into our newly created directory";
chdir (".\\dream.in.code");
std::cout << "\nCurrently viewing:\n%s\n" << getcwd(NULL,0);
listdir (".");
chdir ("..\\"); // move back one
rmdir (".\dream.in.code");
listdir (".");
std::cin.get (); // pause for input
return EXIT_SUCCESS; // program was executed successfully
}
void listdir (const char *path)
{
// first off, we need to create a pointer to a directory
DIR *pdir = NULL; // remember, it's good practice to initialise a pointer to NULL!
pdir = opendir (path); // "." will refer to the current directory
struct dirent *pent = NULL;
if (pdir == NULL) // if pdir wasn't initialised correctly
{ // print an error message and exit the program
printf ("\nERROR! pdir could not be initialised correctly");
return; // exit the function
} // end if
while (pent = readdir (pdir)) // while there is still something in the directory to list
{
if (pent == NULL) // if pent has not been initialised correctly
{ // print an error message, and exit the program
printf ("\nERROR! pent could not be initialised correctly");
return; // exit the function
}
// otherwise, it was initialised correctly. let's print it on the console:
printf ("%s\n", pent->d_name);
}
// finally, let's close the directory
closedir (pdir);
}