Linux 如何在C程序中列出目录中的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4204666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list files in a directory in a C program?
提问by cemal
I'm trying to write an ftp server on Linux. In this matter how can I list files in the directory on terminal by a C program? Maybe I can use exec function to run find command but I want file name as a string to send client program. How can I do this?
我正在尝试在 Linux 上编写一个 ftp 服务器。在这种情况下,我如何通过 C 程序列出终端目录中的文件?也许我可以使用 exec 函数来运行 find 命令,但我想要文件名作为字符串发送客户端程序。我怎样才能做到这一点?
Thanks for answers.
感谢您的回答。
回答by Jean-Bernard Jansen
An example, available for POSIX compliant systems :
一个适用于 POSIX 兼容系统的示例:
/*
* This program displays the names of all files in the current directory.
*/
#include <dirent.h>
#include <stdio.h>
int main(void) {
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d) {
while ((dir = readdir(d)) != NULL) {
printf("%s\n", dir->d_name);
}
closedir(d);
}
return(0);
}
Beware that such an operation is platform dependant in C.
请注意,此类操作在 C 中依赖于平台。
Source : http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046380353&id=1044780608
来源:http: //faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046380353&id=1044780608
回答by Kamiccolo
One tiny addition to JB Jansen's answer- in the main readdir()
loop I'd add this:
JB Jansen 的答案的一个小补充- 在主readdir()
循环中,我会添加以下内容:
if (dir->d_type == DT_REG)
{
printf("%s\n", dir->d_name);
}
Just checking if it's really file, not (sym)link, directory, or whatever.
只是检查它是否真的是文件,而不是(符号)链接、目录或其他任何东西。
NOTE: more about struct dirent
in libc
documentation.
注:更多有关struct dirent
的libc
文件。
回答by Faroq AL-Tam
Here is a complete program how to recursively list folder's contents:
这是一个如何递归列出文件夹内容的完整程序:
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#define NORMAL_COLOR "\x1B[0m"
#define GREEN "\x1B[32m"
#define BLUE "\x1B[34m"
/* let us make a recursive function to print the content of a given folder */
void show_dir_content(char * path)
{
DIR * d = opendir(path); // open the path
if(d==NULL) return; // if was not able return
struct dirent * dir; // for the directory entries
while ((dir = readdir(d)) != NULL) // if we were able to read somehting from the directory
{
if(dir-> d_type != DT_DIR) // if the type is not directory just print it with blue
printf("%s%s\n",BLUE, dir->d_name);
else
if(dir -> d_type == DT_DIR && strcmp(dir->d_name,".")!=0 && strcmp(dir->d_name,"..")!=0 ) // if it is a directory
{
printf("%s%s\n",GREEN, dir->d_name); // print its name in green
char d_path[255]; // here I am using sprintf which is safer than strcat
sprintf(d_path, "%s/%s", path, dir->d_name);
show_dir_content(d_path); // recall with the new path
}
}
closedir(d); // finally close the directory
}
int main(int argc, char **argv)
{
printf("%s\n", NORMAL_COLOR);
show_dir_content(argv[1]);
printf("%s\n", NORMAL_COLOR);
return(0);
}
回答by Joy
Below code will only print files within directory and exclude directories within given directory while traversing.
下面的代码将只打印目录中的文件,并在遍历时排除给定目录中的目录。
#include <dirent.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include<string.h>
int main(void)
{
DIR *d;
struct dirent *dir;
char path[1000]="/home/joy/Downloads";
d = opendir(path);
char full_path[1000];
if (d)
{
while ((dir = readdir(d)) != NULL)
{
//Condition to check regular file.
if(dir->d_type==DT_REG){
full_path[0]='##代码##';
strcat(full_path,path);
strcat(full_path,"/");
strcat(full_path,dir->d_name);
printf("%s\n",full_path);
}
}
closedir(d);
}
return(0);
}