Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8508771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:15:10+00:00 2026-06-11T03:15:10+00:00

I am trying to list all the files in a certain directory on a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:15:12+00:00Added an answer on June 11, 2026 at 3:15 am

    Use stat. You’ll get a struct stat with an st_mode field, and macros S_ISDIR/S_ISREG:

    isDirectory = S_ISDIR(statBuf.st_mode);
    isFile = S_ISREG(statBuf.st_mode);
    

    See man 2 stat. Include sys/stat.h.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to list all the files included in folder using following php
I am trying to get a list of all files in a directory by
I'm trying to list all files in a virtual directory and it's sub directories.
I am trying to get a list of all files in a directory and
I am trying to list all alias files in Mac Osx using the Qt.
I'm trying to figure out how to list all files of an FTP folder
I am trying to copy all the files in a list of directories and
Im trying to list all categories in a VPP-folder using a module from Meridium
I am trying to get the code to list all the directories in a
I'm trying to get a list of all the js files that changed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.