A common thing beginners want to know is how to handle directories in C. The C standard does not provide any way to do this, but it can be achieved using the C POSIX Library.
In this first section, you will be shown how to read the contents of a directory.
First, include the following header file:
#include <dirent.h>
There are a few types you need to familiarize yourself with when working with directories. First type:
DIR
The DIR type represents a directory stream. It is usually passed to functions as a pointer. Second type:
struct dirent
This represents the current position in a directory stream. Depending on your environment, struct dirent can have many members, but the only one we're interested in now is:
char d_name[]
This is the name of the current file.
Now that you know the types, you can use functions in order to obtain them.
DIR* opendir(const char*)
opendir() opens a directory stream. It takes the directory name as it's only argument and returns a pointer to DIR. opendir() returns NULL when it has an error.
struct dirent* readdir(DIR*)
readdir() reads the directory stream provided as the first argument, and it returns a pointer to a dirent struct. It returns NULL after reading the directory stream.
int closedir(DIR*)
closedir() closes the directory stream provided in the first argument. It returns -1 when it has an error.
/* This program lists directory contents */ #include <stdio.h> #include <dirent.h> int main(int argc, char **argv) { char *dirname; DIR *dir; struct dirent *dp; /* read the first argument as the * directory name to list, the default * is to list the current directory */ if (argc<=1) { dirname="."; } else { dirname=argv[1]; } /* Open directory */ dir=opendir(dirname); if (dir==NULL) { perror("Error opening directory"); return 1; } /* List directory contents */ while ((dp=readdir(dir))!=NULL) { /* Remember, d_name is the name of the current file */ printf("%s\n", dp->d_name); } /* Close the directory */ if (closedir(dir)==-1) { perror("Error closing directory"); return 1; } return 0; }
This section will teach you how to get the full path name for the current working directory.
The only header file you will need for this is:
#include <unistd.h>
Likewise, there is only one function you will need:
char* getcwd(char*, size_t)
getcwd() takes two arguments. The first argument is a string that have the current working working directory written to it. The second argument is the size of the string. getcwd() will return the current working directory, and it will return NULL when there is an error.
This section will show you how to change the current working directory.
Once again, all you need is:
#include <unistd.h>
int chdir(const char*)
chdir() takes the directory to change to as it's only argument. This function returns -1 on error.
/* Change current working directory */ #include <stdio.h> #include <unistd.h> int main(int argc, char **argv) { if (argc>1) { if (chdir(argv[1])==-1) { perror("Error changing directory"); return 1; } } else if (argc==1) { printf("Usage: %s <directory>", argv[0]); return 1; } else { return 1; } return 0; }
There are two headers you need to include:
#include <sys/stat.h> #include <sys/types.h>
int mkdir(const char*, mode_t)
mkdir() takes two arguments. The first argument is the name of the directory to create. The second argument sets the file permission bits. If you don't know what to put for the second argument, 0755 is usually the best choice. mkdir() returns -1 on error.
/* Creates a directory */ #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> int main(int argc, char **argv) { if (argc>1) { if (mkdir(argv[1], 0755)==-1) { perror("Error creating directory"); return 1; } } else if (argc==1) { printf("Usage: %s <directory>", argv[0]); return 1; } else { return 1; } return 0; }
This is the only header you need:
#include <unistd.h>
int rmdir(const char*)
rmdir() takes the directory to remove as it's only argument. Note that rmdir() will only remove empty directories. If you want to removed filled directories, you must first delete all of the files they contain. rmdir() returns -1 on error.
Renaming directories is exactly the same as renaming files in C. Consequently, renaming directories is in the C Standard, and should work on all operating systems with conforming C compilers.
The only header needed is:
#include <stdio.h>
int rename(const char*, const char*)
rename() takes two arguments. The first argument is the file to rename. The second argument is what the file will be renamed to. rename() returns 0 on success, and nonzero on failure.
/* This program will rename a file or directory */ #include <stdio.h> int main(int argc, char **argv) { if (argc>2) { if (rename(argv[1], argv[2])!=0) { perror("Error renaming"); return 1; } } else if (argc==2||argc==1) { printf("Usage: %s <file> <renamedfile>", argv[0]); return 1; } else { return 1; } return 0; }