c++ - 如何检查目录是文件还是文件夹?

标签 c++ file directory dirent.h

<分区>

好的,所以我使用的是 mingW,直接结构没有名为 d_type 或 stat、d_stat 或 dd_stat 的变量。我需要知道如何使用我的直接结构来确定我拥有的是文件还是文件夹。这是我的代码。

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

(^_^)

关于c++ - 如何检查目录是文件还是文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3844546/

相关文章:

c++ - 使用 gdb 调试 C++ STL/Boost 的最佳实践

c++ - 如何获取程序执行打开和关闭的文件列表?

c - 如何查看线路是否结束?

file - 根据名称在终端中移动内容

c++ - 在插入其他线程时有效地遍历 map

c++ - 不允许最大化/显示 QML 中的应用程序

c++ - QTreeView、QItemDelegate 和刷新其他项目?

file - 如何在 MS Teams 中 move 文件夹

file - 遍历文件夹并检查是否存在某个文件

c++ - 好的 C++ 目录和文件库?