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 6690611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:39:25+00:00 2026-05-26T05:39:25+00:00

quick question really that has me momentarily stumped. This program searches through a a

  • 0

quick question really that has me momentarily stumped. This program searches through a a file directory and all of its sub directories. When it gets to a file that’s not a directory type I want to open the file, throw it in a buffer and compare it to another file which will already be in another buffer. The problem is that the file fails to open giving me an errno that the file or directory does not exist. My assumption is that is it trying to open the file by only its file name rather than the whole path. How would I pull in the whole path then? I have tried a few things which ultimately lead to compiling errors. can anyone give me a quick pointer?

#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <cctype>
#include <cstdio>
#include <string>
#include <list>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

using std::string;
using std::ostream;
using std::list;
using std::endl;

off_t tell(int fd) {
    return lseek(fd, 0, SEEK_END);
}

void dir_traverse(const std::string& path, std::ostream& out) {
    list<string> child_directories;
    DIR*dirp = opendir(path.data());
    struct dirent*dir_entry = readdir(dirp);
    while(dir_entry !=NULL){ 
        unsigned char d_type = dir_entry->d_type==DT_DIR?'D' : 'F';
        if(d_type == 'D'){ 
            if(dir_entry->d_name[0]!= '.') {
                child_directories.push_back(dir_entry->d_name);
                out<<'\t'<<d_type<<":"<<dir_entry->d_name<<endl;
            }
        }
        if(d_type == 'F'){ 

            int fd= open(dir_entry->d_name, O_RDONLY);
            if(fd =-1){
            out<<"file did not open"<<'\t'<<errno<<endl;
            }
            int size= tell(fd);

            out<<'\t'<<d_type<<":"<<dir_entry->d_name<<endl;

            close(fd);

            //open file
            //read file
            //compare two files
            //print name of file and path if two are equal otherwise do nothing

        }
        dir_entry= readdir(dirp);
    }
    list<string>::iterator it = child_directories.begin();
    while(it != child_directories.end()) {
        dir_traverse(path + "/" + *it, out);
        it++;
    }
    closedir(dirp);
}

int main() {
    dir_traverse("./homework", std::cout);
}
  • 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-26T05:39:26+00:00Added an answer on May 26, 2026 at 5:39 am

    Concatenate them:

    open((path + "/" + dir_entry->d_name).c_str(), ...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a quick question really, I've inherited a website that uses the Global.asax
Really quick jQuery question... I have this function: $(document).ready(function() { $('a#show1').click(function() { $('.item1').toggle(1000); return
Quick question. I have been Googling this all morning, but it's either not there,
Really quick question - (I hope!) I have a div that doesnt display until
Just a quick question if someone has already that kind of setup or if
Quick question. What do you think, I have a few sites that use a
Quick question. There is a legacy website (that is not under my control and
Quick question: If I have a very large function/sub in a class that is
This question actually has two parts. The first part: I've been developing my first
this is a really stupid question but I have a deadline and I'm very

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.