开发者

How do I check if a directory is a file or folder? [duplicate]

开发者 https://www.devze.com 2023-01-18 05:59 出处:网络
This question already has answers here: Accessing Directories in C (3 answers) Closed 7 years ago. Okay, so I\'m using mingW, and the direct struct has no variables named d_type or stat,
This question already has answers here: Accessing Directories in C (3 answers) Closed 7 years ago.

Okay, so I'm using mingW, and the direct struct has no variables named d_type or stat, d_stat, or dd_stat. I need to know how I can use my direct struct to figure out if what I have is a file or folder. Here is my code.

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

/*function... might want it in some class?*/
int getdir (string dir, vector<string> &files)
{
    DIR *dp;
    struct stat _buf;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << 开发者_如何学Python"Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {

        if(stat(dirp->d_name, &_buf) != 0x4)
        files.push_back(string(dirp->d_name));
    }
    closedir(dp);
    return 0;
}

int main()
{
    string dir = string(".");
    vector<string> files = vector<string>();

    getdir(dir,files);

    for (unsigned int i = 0;i < files.size();i++) {
        cout << files[i] << endl;
    }
    return 0;
}



boost::filesystem::is_directory()

//I found it )  

//So, also you can try to call stat() function. ( on Windows ) 

(^_^)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号