主题:[讨论]谁有opendir()和readdir()的具体用法?
xll622
[专家分:240] 发布于 2007-07-18 13:58:00
谁有opendir()和readdir()的具体用法?
最近在学习unix下的常用函数及命令,请大家多指教
最好带简单例子的,因为初学,便于理解
回复列表 (共2个回复)
沙发
jackin0627 [专家分:1270] 发布于 2007-07-18 16:44:00
用 man opendir 和 man readdir 查看
板凳
CLO [专家分:2000] 发布于 2007-07-19 14:20:00
A simple example , achieve DIR command
[code=c]
/* Use Dev-C++ 4.9.9.2 compiled */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
void do_dir (char dirname[])
{
DIR *dir_p;
struct dirent *direntp;
if ((dir_p = opendir (dirname)) == NULL)
{
printf ("dir ERROR !!\n");
exit (0);
}
else
{
while ((direntp = readdir (dir_p)) != NULL)
{
printf ("%s\n", direntp->d_name);
}
closedir (dir_p);
}
}
int main(int argc,char *argv[])
{
if (argc == 1)
{
do_dir(".");
}
else
{
while (argc-->1)
{
printf ("DIR is %s\n\n",*++argv);
do_dir (*argv);
}
}
}
[/code]
我来回复