gpt4 book ai didi

c++ - 使用 dirent 提取文件名

转载 作者:行者123 更新时间:2023-11-30 02:08:52 26 4
gpt4 key购买 nike

你能告诉我,如何在下面的代码中只将文件名(没有目录)放入我的 vector 中吗?:

int getDir (string dir, vector<string> &files)
{
DIR *dp;
struct dirent *dirp;

if ((dp = opendir(dir.c_str())) == NULL) {
cout << "Error (" << errno << ") with " << dir << endl;
return errno;
}

while ((dirp = readdir(dp)) != NULL) {
cout << dirp << endl;
files.push_back(string(dirp->d_name));
}

closedir(dp);
return 0;
}

最佳答案

您可以使用 stat() ,并使用 S_ISDIR 宏检查 st_mode 字段。

关于c++ - 使用 dirent 提取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6059731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com