您的位置:首页 > 运维架构 > Linux

Linux中C/C++对目录的读取

2016-01-28 14:03 302 查看
在Linux中,对于读取普通文件和目录文件有一些不一样的地方。

废话不多说,直接上代码吧。

Show me the code!

#include <iostream>
#include <dirent.h>
using namespace std;
int main() {
string dirName;
cin >> dirName;
DIR *dirPointer;//point the directory file
struct dirent *filePointer;//point a file of the directory
dirPointer = opendir(dirName.c_str());
if (!dirPointer) {
cout << "Error opening!" << endl;
}
else {
while (filePointer = readdir(dirPointer)) {
cout << string(filePointer->d_name) << endl;
}
closedir(dirPointer);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux