Read files in a folder

I am using dir object from dirent.h to read files of a folder. The code I am using in order to read all data is the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vector<string> directories;

	DIR *pDIR;
	const char * c = path.c_str();
	struct dirent *entry;
	if( pDIR=opendir(c) ){
		while(entry = readdir(pDIR)){
			if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 )
				//cout << entry->d_name << "\n";
				directories.push_back( entry->d_name);
		}
		closedir(pDIR);
	}

	//        for (int i = 0; i<directories.size(); i++){
	//            cout << directories[i] << endl;
	//        }

	return directories;


I am wandering when I am trying to read two times the files of the same folder, it will be stored with the same order or every time I ll read in folder it ll return different file order?
The order is not defined. However, as it happens, file systems tend to return things in the same order, but it won't be if the directory was restored from a backup for example.

Mounted partiions? I dunno what happens there. But as members of a union can change etc.

You aren't checking the dirent type in your code. You'll need to if you want to identify symlinks, directories etc.
Topic archived. No new replies allowed.