windows和linux获取文件夹内容的一种方法
来源:互联网 发布:软件测试全程项目 编辑:程序博客网 时间:2024/06/04 20:07
windows:
使用头文件:<io.h>、<direct.h>
关键函数:_findfirst、_findnext
关键结构体:_finddata_t
_finddata_tfileinfo;intptr_t hFile;string root;root.assign(Path);//文件夹绝对路径int len = root.length();if ( !IsEndObliqueLine(Path) )//路径的结尾不是‘\’{root.append("\\");}root.append("*");//路径下的所有文件hFile = _findfirst(root.c_str(), &fileinfo);if ( -1 == hFile){assert(false);return;}strcpy(m_CurrentPath, Path);//保存到m_CurrentPath//清空之前Path的信息FilesPathVector.clear();//自定义的保存信息的容器do //保存信息{LISTCMD_INFO TmpDirInfo = {0};TransFileInfo(fileinfo, TmpDirInfo);FilesPathVector.push_back(TmpDirInfo);}while(_findnext(hFile, &fileinfo) == 0);
linux:
使用头文件: <dirent.h>、 <sys/types.h>、<sys/stat.h>
关键函数:readdir、lstat
关键结构体:dirent
DIR *dir;if ( !(dir = opendir(Path)) ){assert(false);return;} strcpy(m_CurrentPath, Path);//保存到m_CurrentPathstruct dirent *d_ent;char fullpath[128];FilesPathVector.clear();while ( (d_ent = readdir(dir)) != NULL ){struct stat file_stat;//if ( strncmp(d_ent->d_name, ".", 1) == 0 )//{//continue;// 忽略"."目录//}memset(fullpath, '\0', sizeof(fullpath));strcpy( fullpath, Path);if ( !strcmp(fullpath, "/") ){fullpath[0] = '\0';}strcat(fullpath, "/");strcat(fullpath, d_ent->d_name);if ( lstat(fullpath, &file_stat) < 0 ){assert(false);return;}//保存信息到自己的数据结构,在函数外面保存文件名LISTCMD_INFO TmpDirInfo = {0};strcpy(TmpDirInfo.cFileName, d_ent->d_name);TransFileInfo(&file_stat, TmpDirInfo);FilesPathVector.push_back(TmpDirInfo);}closedir(dir);
值得注意的是,此种方法windows的路径分隔是用'\',而linux用'/',使用时多少有点不便。
如果有其他更好得方法,欢迎交流!
- windows和linux获取文件夹内容的一种方法
- windows和linux获取文件夹内容的一种方法
- windows和linux获取文件夹内容的一种方法
- 一种在windows和linux间传文件的方法
- Windows和Linux下枚举文件夹下文件的方法
- Windows和Linux下枚举文件夹下文件的方法
- 从linux 系统 中获取 windows系统的文件的 一种方法
- Linux访问Windows共享文件夹的方法
- linux访问windows共享文件夹的方法
- Windows开发--获取文件大小及获取文件夹大小的方法
- 用fopen判断文件和文件夹的一种方法
- linux下 c语言递归遍历文件夹下所有文件和子文件夹(附上替换文本文件内容的方法)
- 获取文件和文件夹的方法
- 使用Spring的ResourceLoader获取文件夹在主机的实际位置(windows和linux通用)
- 在linux和windows下获取路径的常用方法
- wxWidgets整合OpenGL+OpenCV+MathPlot(一种Windows和Linux通用的方法)
- 获取和设置控件内容的方法
- 获取svg和svg内容的方法
- EllysXors_求L-R中所有数异或最后的值
- pthread互斥信号量使用总结
- php脚本的执行过程(编译与执行相分离)
- 黑马训练营-位移运算
- 连接池有那几种 分别有什么区别
- windows和linux获取文件夹内容的一种方法
- 面向对象——接口(一)
- 修改Windows下的Memcached下的服务端口
- CSS的未来:一些试验性CSS属性
- 1022. Genealogical Tree
- sysbench-一个系统及mysql性能测试的好工具!
- 标准C语言库 Glibc 2.15
- Android重要组件使用方法汇总
- UESTC :吴神的战役