如何检查目录是否是文件或文件夹? [英] How do I check if a directory is a file or folder?

查看:184
本文介绍了如何检查目录是否是文件或文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我使用mingW,直接结构没有变量名为d_type或stat,d_stat或dd_stat。我需要知道如何使用我的直接结构,弄清楚如果我有一个文件或文件夹。这是我的代码。

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 << "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 ) 

(^_^)

这篇关于如何检查目录是否是文件或文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆