I am trying to list all the files in a certain directory on a shared drive using the following code:
#include <iostream>
#include <string>
#include "dirent.h"
int main ()
{
DIR *directoryHandle = opendir("./temp/");
if (NULL != directoryHandle)
{
dirent *entry = readdir(directoryHandle);
while (NULL != entry)
{
//skip directories and select only files (hopefully)
if ((DT_DIR != entry->d_type) && (DT_REG == entry->d_type || DT_UNKNOWN == entry->d_type))
{
std::cout << "Name: " << entry->d_name << " Type:" << std::to_string(entry->d_type) << std::endl;
}
//go to next entry
entry = readdir(directoryHandle);
}
closedir(directoryHandle);
}
return 0;
}
The problem is that entry->d_type contains DT_UNKNOWN for directories as well as for files in the ./temp/
directory.
Is there any (reliable) linux-specific way to try and read each entry and determine if it's a file or directory?
The output of cat /etc/SuSE-release
is:
SUSE Linux Enterprise Desktop 11 (x86_64) VERSION = 11 PATCHLEVEL = 1
The Linux version is: 2.6.32.59-0.7-default
Still, I expect this code to work on other platforms as well.