windows和linux获取文件夹内容的一种方法
2012-05-21 19:29
501 查看
windows:
使用头文件:<io.h>、<direct.h>
关键函数:_findfirst、_findnext
关键结构体:_finddata_t
linux:
使用头文件: <dirent.h>、 <sys/types.h>、<sys/stat.h>
关键函数:readdir、lstat
关键结构体:dirent
值得注意的是,此种方法windows的路径分隔是用'\',而linux用'/',使用时多少有点不便。
如果有其他更好得方法,欢迎交流!
使用头文件:<io.h>、<direct.h>
关键函数:_findfirst、_findnext
关键结构体:_finddata_t
_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:
使用头文件: <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_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用'/',使用时多少有点不便。
如果有其他更好得方法,欢迎交流!
相关文章推荐
- windows和linux获取文件夹内容的一种方法
- windows和linux获取文件夹内容的一种方法
- 从linux 系统 中获取 windows系统的文件的 一种方法
- windows及Linux系统下获取IP及MAC地址的方法
- ECLIPSE插件获取到的有关“当前选择项”内容的一种典型实现方法
- linux挂载windows分区和linux下带空格文件夹名表示方法
- linux访问windows共享文件夹的两种方法
- 呵呵,Windows系统下获取本机ip的方法和Linux系统下获取本机ip的方法
- Windows下Debug Linux C/C++程序的一种方法
- Windows和Linux下枚举文件夹下文件的方法
- 清除本机在Windows下访问共享文件夹时的登录信息的一种方法
- Python获取动态网页内容的一种处理方法
- 利用C/C++编写程序以获取文件夹内所有子文件名,windows和Linux两个版本都有
- 在linux和windows下获取路径的常用方法
- 在Linux与Windows上获取当前堆栈信息的方法
- Windows 8 Metro/Windows UI 应用获取屏幕分辨率的一种方法。
- VMware中Linux与Windows共享文件的一种简单好用方法
- C#实现从windows剪贴板获取内容的方法
- (转)linux访问windows共享文件夹的两种方法
- Windows开发--获取文件大小及获取文件夹大小的方法