
让我们考虑以下 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 <iostream>
#include <dirent.h>
#include <sys/types.h>
using namespace std;
int main(void) {
DIR *dr;
struct dirent *en;
dr = opendir("."); //open all directory
if (dr) {
while ((en = readdir(dr)) != NULL) {
cout<<" \n"<<en->d_name; //print all directory name
}
closedir(dr); //close all directory
}
return(0);
}BINSEARC.C BINTREE (1).C BINTREE.C BTREE.C BUBBLE.C c.txt file3.txt HEAP.C HEAPSORT.C HLINKLST.C INSERTIO.C LINKLIST.C LINKLST.C LLIST1.C players.cpp PolarRect.cpp QUEUE.C
#include <stdio.h>
#include <dirent.h>
int main(void) {
DIR *dr;
struct dirent *en;
dr = opendir("."); //open all or present directory
if (dr) {
while ((en = readdir(dr)) != NULL) {
printf("%s\n", en->d_name); //print all directory name
}
closedir(dr); //close all directory
}
return(0);
}BINSEARC.C BINTREE (1).C BINTREE.C BTREE.C BUBBLE.C c.txt file3.txt HEAP.C HEAPSORT.C HLINKLST.C INSERTIO.C LINKLIST.C LINKLST.C LLIST1.C
以上就是如何使用C或C++获取目录中的文件列表?的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号