我正在尝试在 Linux 上编写一个 ftp 服务器。在这件事上,如何通过 C 程序列出终端目录中的文件?也许我可以使用 exec 函数来运行 find 命令,但我希望文件名作为字符串发送客户端程序。我该怎么做?
感谢您的回答。
最佳答案
一个适用于 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);
}
请注意,这样的操作在 C 中依赖于平台。
来源:http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046380353&id=1044780608
https://stackoverflow.com/questions/4204666/