linux下的c 编程------dir scan
2004-11-08 21:06
302 查看
<*
来源:http://www.51one.net
*>
#include<unistd.h>
#include<stdlib.h>
#include<dirent.h>
#include<sys/stat.h>
#include<string.h>
#include<stdio.h>
void printdir(char*dir,int depth)
{
DIR*dp ;
struct dirent*entry ;
struct stat statbuf ;
if((dp=opendir(dir))==NULL)
{
fprintf(2,"can not open directory :%s/n",dir);
return ;
}
chdir(dir);
while((entry=readdir(dp))!=NULL)
{
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(strcmp(".",entry->d_name)==0||strcmp("..",entry->d_name)==0)
{
continue ;
}
printf("%*s%s//n",depth,"",entry->d_name);
printdir(entry->d_name,depth+4);
}
else printf("%*s%s/n",depth,"",entry->d_name);
}
chdir("..");
closedir(dp);
}
int main()
{
printf("Directory scan of /home/lzy/prog_c:/n");
printdir("/home/lzy/prog_c",0);
printf("done!");
exit(0);
}
来源:http://www.51one.net
*>
#include<unistd.h>
#include<stdlib.h>
#include<dirent.h>
#include<sys/stat.h>
#include<string.h>
#include<stdio.h>
void printdir(char*dir,int depth)
{
DIR*dp ;
struct dirent*entry ;
struct stat statbuf ;
if((dp=opendir(dir))==NULL)
{
fprintf(2,"can not open directory :%s/n",dir);
return ;
}
chdir(dir);
while((entry=readdir(dp))!=NULL)
{
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(strcmp(".",entry->d_name)==0||strcmp("..",entry->d_name)==0)
{
continue ;
}
printf("%*s%s//n",depth,"",entry->d_name);
printdir(entry->d_name,depth+4);
}
else printf("%*s%s/n",depth,"",entry->d_name);
}
chdir("..");
closedir(dp);
}
int main()
{
printf("Directory scan of /home/lzy/prog_c:/n");
printdir("/home/lzy/prog_c",0);
printf("done!");
exit(0);
}
相关文章推荐
- linux下的c 编程------dir scan
- APUE读书笔记-00预备知识(03)-Linux下C语言编程简介
- 我是这样学习Linux下C语言编程的-把程序输出信息加到系统日志里去
- linux下socket编程 select实现非阻塞模式多台客户端与服务器通信
- linux驱动之模块化编程
- Linux下使用C做简单的网络编程
- [综合面试] 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- c++ 学习笔记(高级linux编程) day5
- Socket 编程 windows到Linux代码移植遇到的问题
- linux下bluetooth编程(三)HCI层编程
- Linux 高性能服务器编程——I/O复用
- [linux] C语言Linux系统编程-捕获进程信号
- LINUX 系统编程之文件IO
- linux网络高级编程之非阻塞
- Linux串口编程
- Linux下的文件编程
- 高手必读:Linux编程经典书籍
- Linux 高级网络编程
- 【zz】Linux音频编程指南
- linux下socket编程:I/O模型