如何检查目录是文件还是文件夹?
好的,所以我使用 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(^_^)
(^_^)