腾讯面试:Linux下如何扫描目录
来源:互联网 发布:党规党纪面前知敬畏 编辑:程序博客网 时间:2024/06/06 00:30
问题:在linux下,输出某个文件夹下的文件即其子目录,以及子目录下的文件和文件夹。
代码如下:
#include<iostream>#include<unistd.h>#include<sys/stat.h>#include<sys/types.h>#include<dirent.h>#include<fcntl.h>#include<string.h>/*值得注意的是: struct stat statBuf; 1.如果声明为 struct stat * statBuf,后面用指针方式操作的时候,总是出错。 2.必须使用chdir,否则不在文件所在目录工作的话,S_ISDIR会将文件判定为文件夹而引发错误。 */using namespace std;int printDir(const char * dirPath){ DIR * dir; struct dirent * entry; struct stat statBuf; if ((dir=opendir(dirPath))==NULL) { cout<<"error when open the directory!"<<endl; return -1; } else { chdir(dirPath); while((entry=readdir(dir))!=NULL) { lstat(entry->d_name,&statBuf); if (S_ISDIR(statBuf.st_mode)) { if ((strcmp(".",entry->d_name)==0)||(strcmp("..",entry->d_name)==0)) continue; cout<<"this is a directory"<<entry->d_name<<endl; printDir(entry->d_name); } else cout<<"this is a document"<<entry->d_name<<endl; } chdir(".."); closedir(dir); }}int main(){ printDir("."); return 0;}
- 腾讯面试:Linux下如何扫描目录
- linux下目录扫描的实现
- linux扫描目录下所有文件
- linux目录扫描
- linux下如何删除目录
- linux下如何删除目录
- Linux下如何删除目录
- Linux下的目录扫描操作函数使用实践
- Linux目录扫描:打印指定目录下的所有文件及目录
- 扫描目录下所有文件
- Linux文件操作-->扫描目录
- linux c 扫描文件目录
- Linux 下如何强制取消 NFS 目录
- Linux下如何删除非空目录
- Linux下如何删除非空目录
- Linux下如何删除非空目录
- Linux下如何删除非空目录
- Linux下如何修改MySQL物理目录
- 在tomcat中配置web应用程序
- ASCII码对照表
- JAVA虚拟机工作原理
- iPhone-NSAssert使用
- 在tomcat中配置web应用程序
- 腾讯面试:Linux下如何扫描目录
- ftp shell扫描目录上传脚本
- Java程序员的成长过程
- 关于消息提示框中换行的问题
- PHP模拟POST提交的2种方法 curl,fsockopen,HttpClient
- 字符串操作(一)
- Linux下PHP开发环境搭建-php,mysql,apache
- android学习笔记转--------------junit.framework.AssertionFailedError: No tests found in错误解决办法
- 马拦过河卒(NOIP2002)