如何列出一个C程序目录中的文件? [英] How to list files in a directory in a C program?
本文介绍了如何列出一个C程序目录中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试着写在Linux上的FTP服务器。在这件事情我怎么能由一个C程序列表在终端目录中的文件?也许我可以使用exec函数来运行find命令,但我想文件名作为字符串发送客户端程序。我怎样才能做到这一点?
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?
感谢您的答案。
推荐答案
一个例子,可用于POSIX兼容的系统:
An example, available for POSIX compliant systems :
/*
* 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.
来源:<一个href=\"http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046380353&id=1044780608\">http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046380353&id=1044780608
这篇关于如何列出一个C程序目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文