Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

I am getting error can not open include file dirent.h .No
such file or directory . the code is given below
I am trying to use directory scanning code A single directory have multiple files i want to scan them all step by step


C++
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
 #include <sys/stat.h> /* for stat() */


using namespace std;
int isDir(string path)
;

/*function... might want it in some class?*/
int getdir (string dir, vector<string> &dirlist, vector<string> &fileList)
{
    DIR *dp;
    struct dirent *dirp, *dirFp ;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        if (strcmp (dirp->d_name, ".") != 0 && strcmp(dirp->d_name, "..") != 0) {
            //dirlist.push_back(string(dirp->d_name));

            string Tmp = dir.c_str()+ string("/") + string(dirp->d_name);
            if(isDir(Tmp)) {
            //if(isDir(string(dir.c_str() + dirp->d_name))) {
                dirlist.push_back(Tmp);
                getdir(Tmp,dirlist,fileList);
            } else {
  //              cout << "Files :"<<dirp->d_name << endl;
                fileList.push_back(string(Tmp));
            }

        }
    }
    closedir(dp);
    return 0;
}

int isDir(string path)
{
    struct stat stat_buf;
    stat( path.c_str(), &stat_buf);
    int is_dir = S_ISDIR( stat_buf.st_mode);
//    cout <<"isDir :Path "<<path.c_str()<<endl;
    return ( is_dir ? 1: 0);
}

int main()
{
    string dir = string("/test1/mfs");
    vector<string> dirlist = vector<string>();
    vector<string> fileList = vector<string>();

    getdir(dir,dirlist,fileList);
#if 0
    for (unsigned int i = 0;i < dirlist.size();i++) {
        cout << "Dir LIst" <<dirlist[i] << endl;
        //string dirF = dir + "/" + dirlist[i];
        //getdir(dirF,fileList);
    }
#endif
    for (unsigned int i = 0; i < fileList.size(); i++)
        cout << "Files :"<<fileList[i]<< endl;
    return 0;
}


plz help whats problem with this code simple header file error is not understand able why this happen
Posted
Updated 8-May-12 19:19pm
v2
Comments
Siddiqui's 9-May-12 1:14am    
What platform or IDE you are using?
prog786 9-May-12 1:24am    
i am using VC6

1 solution

This is because dirent.h doesn't come with VC6.

Only following compiler knows dirent.h

Turbo C++ (DOS)
GCC (Cross-platform)
MinGW (Microsoft Windows)
Borland C++ Builder (Microsoft Windows)


You can download dirent.h to use in VC6, following is the link which may help you

http://www.softagalleria.net/dirent.php[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900