【发布时间】:2018-05-14 14:45:33
【问题描述】:
我想使用 c++ 从目录中获取 txt 文件。我在谷歌搜索并找到“dirent.h”,但我不能使用这个库。它给了我一个 C1083 故障。这是我的代码。我已经包含了 fstream,dirent.h vs...
ifstream fin;
string dir, filepath;
int num;
DIR *dp;
struct dirent *dirp;
struct stat filestat;
cout << "dir to get files of: " << flush;
getline(cin, dir);
dp = opendir(dir.c_str());
if (dp == NULL)
{
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}
while ((dirp = readdir(dp)))
{
filepath = dir + "/" + dirp->d_name;
if (stat(filepath.c_str(), &filestat)) continue;
if (S_ISDIR(filestat.st_mode)) continue;
fin.open(filepath.c_str());
if (fin >> num)
cout << filepath << ": " << num << endl;
fin.close();
}
`
【问题讨论】:
-
我不知道你是否可以使用 C++17,但如果可以,那么你将可以访问标准库中的文件系统库。
-
您是否尝试过查找C1083?你如何包含相应的标题?
-
在 C++17 之前,有提升。比 dirent 容易得多。