腰臀肌筋膜炎的症状:在UNIX/LINUX下编程: 提供指令列出现在的目录所有的文件 怎么编

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 23:58:40

#include <unistd.h>
#include <dirent.h>

int GetList(const char *path); // 获取某路径下所有文件

int main(int argc, char *argv[])
{
char *cwd;

if ((cwd = getcwd(NULL, 255)) == NULL)
{
perror("getcwd");
exit(1);
}
(void)printf("\a当前工作路径是:%s\n", cwd);
GetList(cwd);

return 0;
}

int GetList(const char *path)
{
DIR *dir;
struct dirent *ptr;
if( (dir=opendir(path))==NULL )
{
perror("opendir error");
return -1;
}

while( (ptr=readdir(dir))!=NULL )
{
printf("ptr.name = %s\n", ptr->d_name);
}
closedir(dir);

return 0;
}

调用shell命令:ls-a或者ll