First of all, I'm a complete novice at programming, especially in C. The example I'm going to show is code I've snagged from this site and kicked until it didn't quite do what I wanted. :)
Second, I'm using a dual-core Celeron with Linux Mint and the GNU C Compiler.
What I want to do is list the subdirectories within a directory, skipping the parent directory if any. Unfortunately, the code I have doesn't work. I tried strcpy and strncpy, but I couldn't figure out how to use them and I just screwed up the code so that I got a "segmentation fault" when I tried to run the program.
Here's the code:
/*
* This program displays the names of all files in the current directory.
*/
#include <dirent.h>
#include <string.h>
#include <stdio.h>
int main(void)
{
DIR *d;
struct dirent *dir;
char dirname[255];
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (dir->d_type == DT_DIR)
{
strncpy(dirname, dir->d_name, 254);
dirname[254] = '\0';
if (dirname != "..") /* I want to skip this, but it doesn't work */
{
printf ("%s\n", dirname);
}
}
}
closedir(d);
}
return(0);
}
Thanks for any help you can give. I'd especially be interested in links to sites that can offer tutorials that would help me with this little project, as I want a better understanding of what the heck I'm doing. :)