让我们考虑以下 C++ 示例代码来获取目录中的文件列表。
算法
Begin
Declare a poniter dr to the DIR type.
Declare another pointer en of the dirent structure.
Call opendir() function to open all file in present directory.
Initialize dr pointer as dr = opendir(".").
If(dr)
while ((en = readdir(dr)) != NULL)
print all the file name using en->d_name.
call closedir() function to close the directory.
End.
登录后复制
示例
#include
#include
#include
using namespace std;
int main(void) {
DIR *dr;
struct dirent *en;
dr = opendir("."); //open all directory
if (dr) {
while ((en = readdir(dr)) != NULL) {
cout