如何在指定目录下获取指定文件尾缀的文件名
2015-07-24 16:51
411 查看
C++
使用两个文件file.h和file.cpp
file.h 包含class file的申明
file.cpp 包含class file的实现以及简单的测试主程序
///~file.h
///~file.cpp
主函数之中我指定的是J盘下,后缀名为txt的文件。
以上为全部代码。
其中struct _finddata_t可以参考上面链接:
http://baike.baidu.com/link?url=EF5GReB-JD1P42qZQeEGr8vo42JqFncHWMozCXsafxkjMFubPu7RnqTOMPUPBxt2pBSYTxn7nXhAXt6H5W0nqq
_findfirst函数的用法可以参考一下链接:
http://blog.csdn.net/mxling/article/details/4761067
使用两个文件file.h和file.cpp
file.h 包含class file的申明
file.cpp 包含class file的实现以及简单的测试主程序
///~file.h
#ifndef FILE_H_ #define FILE_H_ #include <iostream> #include <io.h> #include <direct.h> #include <cstring> #include <string> #include <vector> #include <iomanip> #include <ctime> #include <malloc.h> using namespace std; class file{ private: //path of file string path; public: //constructor file(const string name) { path = name; } //get all file or directory void getFiles(vector<string> &file); //get filename end with str vector<string> getFilesWithStr(string str); string getpath(){ return path;} }; #endif // FILE_H_
///~file.cpp
#define FILE_TEST_ #include "file.h" void file::getFiles(vector<string> &files) { //handle of file long hFile = 0; //information of file struct _finddata_t fileinfo; string p; if((hFile=_findfirst(p.assign(path).append("/*").c_str(),&fileinfo)) != -1) { do{ // if it's a directory, skip it //else take it into vector if(fileinfo.attrib) { if(fileinfo.attrib & _A_SUBDIR) continue; else files.push_back(p.assign(path).append("/").append(fileinfo.name)); } }while(_findnext(hFile,&fileinfo)==0); } } vector<string> file::getFilesWithStr(string str) { vector<string> filelist; vector<string> temp; getFiles(filelist); int n = str.size() + 1; str = "."+str; for(unsigned i = 0; i< filelist.size(); i++) { string str1 = filelist[i]; int pos = str1.size() - n ; //compare str from pos for n letters //you can also look up this method on the internet if(str1.compare(pos,n,str) == 0) { //get str1 into vector temp.push_back(str1); } } return temp; } #ifdef FILE_TEST_ int main() { file path("j:"); vector<string> filelist; path.getFiles(filelist); //this a iterator of vector, it use for traversing vector vector<string>::iterator iter=filelist.begin(); vector<string> temp; temp= path.getFilesWithStr("txt"); iter=temp.begin(); for(;iter!=temp.end();iter++) cout<<(*iter) + "\n"; } #endif // FILE_TEST_
主函数之中我指定的是J盘下,后缀名为txt的文件。
以上为全部代码。
其中struct _finddata_t可以参考上面链接:
http://baike.baidu.com/link?url=EF5GReB-JD1P42qZQeEGr8vo42JqFncHWMozCXsafxkjMFubPu7RnqTOMPUPBxt2pBSYTxn7nXhAXt6H5W0nqq
_findfirst函数的用法可以参考一下链接:
http://blog.csdn.net/mxling/article/details/4761067
相关文章推荐
- Windows内核安全与驱动开发
- 最大流模板(poj3469)
- 疯牛
- leveldb学习:Version
- iOS 企业证书发布app 流程
- 用js实现动态增加行
- 【字符串数据结构后缀系列Part3】后缀自动机的性质和应用
- 钱币兑换问题(完全背包)
- RobotFramework 数据分离
- RobotFramework 数据分离
- DNS及DNS主从配置
- linux 下格式化U盘,制作U盘启动盘
- Android延时执行调用的几种方法(转)
- 最小生成树算法
- http\r\n
- Ubuntu系统下的Hadoop集群(1)_Hadoop安装教程_单机/伪分布式配置
- Spring boot 学习知识点
- jsoncpp-0.5,支持uint64
- angularjs图片上传后不刷新的解决办法
- Shell根据年月日创建文件夹