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

  • Home
  • SEARCH
  • 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 4558512
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:39:37+00:00 2026-05-21T17:39:37+00:00

Hey everyone. I need to write a POSIX program to search through an entire

  • 0

Hey everyone. I need to write a POSIX program to search through an entire file system for a specified file starting at the top directory. I’ve got some code which isn’t done at all, but when I run it, and check to see if a particular file is a directory, it’s saying this file which is not at all a directory is a directory and is trying to move into it, causing an error. I’m not sure how I can tell it that this type of file isn’t a directory.

Here’s my code. I know it’s not perfect and I could probably do some things differently in the way of getting the directory names and passing them into the function. Either way, I’m pretty sure I have to do this recursively.

The file in question is /dev/dri/card0 and I’m running this from a Debian virtual machine.

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
#include <stdint.h>
#include <locale.h>
#include <langinfo.h>
#include <fcntl.h>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std; 

void SearchDirectory(string file_Name, string directory){
    string new_Directory = directory; 
    DIR *dirp; 
    dirp = opendir(directory.c_str()); 
    struct dirent *dptr; 
    struct stat statStruct; 

    while(dptr = readdir(dirp)){
        stat(dptr->d_name, &statStruct); 
        if( S_ISDIR(statStruct.st_mode) ){

            string check = dptr->d_name; 
            if ( check.compare(".") == 0 || check.compare("..") == 0 ){
                continue; 
            }
            else{
                cout << dptr->d_name << " is is a directory" << endl; 
                new_Directory.append("/");
                new_Directory.append(dptr->d_name);  
                SearchDirectory(file_Name, new_Directory); 
            }
        }
        else if( S_ISREG(statStruct.st_mode)){
            string check = dptr->d_name; 
            if( check.compare(file_Name) == 0){
                cout << "Found " << file_Name << " in " << directory << "/" << endl; 
            }
        }
    }
}

int main(int argc, char *argv[]){

    if(argc < 2 || argc > 2){
        cerr << "This program will find the specified file." << endl; 
        cerr << "Usage: mysearch <filename>" << endl; 
        return 1; 
    }

    string file_Name = argv[1]; 
    SearchDirectory(file_Name, "/"); 

    return 0; 

}
  • 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-05-21T17:39:38+00:00Added an answer on May 21, 2026 at 5:39 pm

    ->d_name returns just the name of the file, not the path to the file. You need to stat (not yet constructed) new_Directory instead of dptr->d_name.

    You also have a problem if a directory contains more than one subdirectories. Your construction of new_Directory is incorrect for each subdirectory after the first.

    You never closedir your directory handle, so you run out of resources. You should also consider loading the entire directory into an array before recursing to avoid running out of handles.

    void SearchDirectory(string directory, string target_File_Name){
        DIR *dirp = opendir(directory.c_str());
        if (!dirp) {
            perror(("opendir " + directory).c_str());
            return;
        }
    
        struct dirent *dptr;
        while(dptr = readdir(dirp)){
            string file_Name = dptr->d_name;
            string file_Path = directory + "/" + file_Name;
    
            struct stat statStruct; 
            stat(file_Path.c_str(), &statStruct); 
            if( S_ISDIR(statStruct.st_mode) ){
                if ( file_Name.compare(".") == 0 || file_Name.compare("..") == 0 ){
                    continue; 
                }
    
                SearchDirectory(file_Path, target_File_Name);
            }
            else if( S_ISREG(statStruct.st_mode)){
                if( file_Name.compare(target_File_Name) == 0){
                    cout << file_Path << endl;
                }
            }
        }
    
        closedir(dirp);
    }
    

    Update: Added second problem.

    Update: Added third problem.

    Update: Added code.

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

Sidebar

Related Questions

Hey everyone, I am trying to run the following program, but am getting a
Hey everyone, I have a database result that returns from a method. I need
Hey everyone, Again I need your help :D Is it possible to add the
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey everyone. I'm trying to make a swing GUI with a button and a
Hey everyone, I'm working on a PHP application that needs to parse a .tpl
Hey. I'm taking a course titled Principles of Programming Languages, and I need to
Hey everyone - I am wondering if anyone could help me out - I
Hey everyone. My friend and I are re-learning Visual Basic, and we are stumped
Hey everyone, just a quick thing, I have the hex to integer working, but

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.