你如何获得C中的目录列表? [英] How do you get a directory listing in C?
本文介绍了你如何获得C中的目录列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 C 中扫描目录中的文件夹和文件?它需要是跨平台的.
How do you scan a directory for folders and files in C? It needs to be cross-platform.
推荐答案
以下 POSIX 程序将打印当前目录下的文件名:
The following POSIX program will print the names of the files in the current directory:
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main (void)
{
DIR *dp;
struct dirent *ep;
dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
puts (ep->d_name);
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
return 0;
}
来源:http://www.gnu.org/software/libtool/manual/libc/Simple-Directory-Lister.html
在 Ubuntu 16.04 中测试.
Tested in Ubuntu 16.04.
这篇关于你如何获得C中的目录列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文