Dirent.h - Cannot Detect Parent Directory

Greetings,

I've only just started working with the dirent.h library and I've run into a small issue. I am unable to 'filter out' the current directory "." and the parent directory ".." from my search.

Basically what I'm trying to do is:
I have a list of folders, I want a list of all the contents of these folders.

My algorithm is simple:
For each folder in directory
Attempt to open
If successful list contents
Else try next folder

I have a working version except one thing, its listing "." and ".." even though I have an if statement that checks for them. Are they not strings?

Here is an example of the section of code.

1
2
3
4
5
6
7
  	do{
	    if ((testDir = readdir(test)) != NULL){
		if (testDir->d_name != ".."){
			cout << testDir->d_name << endl;
		}
	    }
	} while (testDir != NULL);


Normally I wouldn't care if it lists the current and parent directories however I append the name of the directory to a new path when I attempt an open and it's grabbing file names from the parent directory which I do not want.

Any advice/help is appreciated,
Thanks
I ended up getting around the issue by copying the name to a string and using strcmp on that instead, here is the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

if ((directory = opendir(pathName.c_str())) == NULL){	//Open directory, tell if failed
		cout << "Error opening directory" << endl;
		return -1;
	}

	
	do{
		if ((dir = readdir(directory)) != NULL){			
			compareString = dir->d_name;
			if (dir->d_type == DT_DIR && strcmp(compareString.c_str(), "..")){
				tmpStr += pathName;
				tmpStr += "\\";
				tmpStr += dir->d_name;
				if ((test = opendir(tmpStr.c_str())) != NULL){
					do{
						if ((testDir = readdir(test)) != NULL){
								compareString = testDir->d_name;
								if (strcmp(compareString.c_str(), ".") && strcmp(compareString.c_str(), "..")){
									cout << testDir->d_name << endl;
								}
						}
					} while (testDir != NULL);
				}
				tmpStr = "";
			}
		}
	} while (dir != NULL);

Topic archived. No new replies allowed.