windows和linux获取文件夹内容的一种方法
来源:互联网 发布:企业信息数据 编辑:程序博客网 时间:2024/06/13 10:03
转载自博客:http://blog.csdn.net/c__allen/article/details/7588446
如有侵犯立即删除
windows:
使用头文件:
_finddata_t fileinfo; 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:
使用头文件:
<span style="white-space:pre"> </span>DIR *dir; if ( !(dir = opendir(Path)) ) { assert(false); return; } <span style="white-space:pre"> </span>strcpy(m_CurrentPath, Path);//保存到m_CurrentPath struct 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用'/',使用时多少有点不便。如果有其他更好得方法,欢迎交流!
0 0
- 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内容的方法
- const,static,extern用法总结
- poj 2446 Chessboard(二分图最大匹配)
- activity生命周期
- linux pwm framwork
- 搭建企业内部yum仓库(centos6+centos7+epel源)
- windows和linux获取文件夹内容的一种方法
- 使用ThumbnailUtils类回获取视频第一帧缩略图
- opencv使用VideoCapture无法打开视频
- VS2008 Tab跟空格切换设置
- vs 无法启动IIS Express Web服务器的终极解决方案
- HBase开发实例学习
- 【Boost】boost::noncopyable介绍
- PHP面向对象编程
- The Frog's Games(二分)