自動檢索當前文件夾下的文件

# include<iostream>
# include<io.h>
# include<string>
# include<direct.h>
# include<vector>

using namespace std;

int main()
{
	//獲取當前路徑:
	char path1[1024];
	getcwd(path1, 1024);
	string path = path1;
	printf("當前路徑爲:%s\n", path.c_str());

	//搜索文件
	string search_file = path + "\\" + "*.bmp";//一定寫成通配符,搜索所有文件寫成"*"
	struct _finddata_t document; //建立查找文件結構體;
	intptr_t index = 0;//用於查找的句柄;在32位系統中不能使用long類型,在64位中可以,intptr_t則通用
	
					   
	//下面的_findfirst 查找成功返回任意值,失敗返回-1; \
	        _findnext  查找成功返回  0,  失敗返回-1; \
			_findclose     成功返回  0,  失敗返回-1;

	vector<string> file_name;
	index = _findfirst(search_file.c_str(), &document);
	while (!_findnext(index, &document))
	{
		file_name.push_back(document.name);
	}
	int count = 1;
	for (auto &ele : file_name)
	{
		printf("%s:\t", (("第" + to_string(count) + "個文件").c_str()));
		cout << ele << endl;
		count++;
	}
    _findclose(handle);
	getchar();
	return 0;
}
/*
注:_finddata_t結構中還含有如下參數:
		struct _finddata_t
			{
			 unsigned attrib;		文件屬性
			 time_t time_create;	創建時間
			 time_t time_access;	訪問時間(最後)
			 time_t time_write;		寫入時間(最後)
			 _fsize_t size;			文件大小(字節)
			 char name[_MAX_FNAME];(文件名長度)
			};
*/

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章