蘇州招聘網(wǎng)站建設(shè)bt磁力搜索
目錄
- 核心功能說明
- 批量讀取代碼
博客長期更新,本文最新更新時間為:2025年6月14日。
核心功能說明
-
文件遍歷機(jī)制
_findfirst()
:啟動文件搜索,返回首個匹配文件句柄_findnext()
:獲取下一個匹配文件_findclose()
:釋放搜索資源
-
關(guān)鍵數(shù)據(jù)結(jié)構(gòu)
struct _finddata_t {unsigned attrib; // 文件屬性(普通/目錄/隱藏等)time_t time_create; // 創(chuàng)建時間time_t time_access; // 訪問時間time_t time_write; // 修改時間_fsize_t size; // 文件大小char name[_MAX_FNAME]; // 文件名 };
-
文件屬性常量
_A_NORMAL // 普通文件 (0x00000) _A_RDONLY // 只讀文件 (0x00001) _A_HIDDEN // 隱藏文件 (0x00002) _A_SYSTEM // 系統(tǒng)文件 (0x00004) _A_SUBDIR // 子目錄 (0x00010) _A_ARCH // 存檔文件 (0x00020)
批量讀取代碼
#include <iostream>
#include <string>
#include <vector>
#include <io.h> // Windows文件操作頭文件
using namespace std;vector<string> getFilesByExtension(const string& folder, const string& ext) {vector<string> fileList;_finddata_t fileInfo;// 構(gòu)造搜索模式:目錄 + 通配符 + 后綴string searchPattern = folder + "\\*" + ext;// 開始搜索long handle = _findfirst(searchPattern.c_str(), &fileInfo);if (handle == -1L) {cerr << "未找到文件: " << searchPattern << endl;return fileList;}do {// 跳過"."和".."目錄if (strcmp(fileInfo.name, ".") == 0 || strcmp(fileInfo.name, "..") == 0) continue;// 排除子目錄(只保留文件)if (!(fileInfo.attrib & _A_SUBDIR)) {string fullPath = folder + "\\" + fileInfo.name;fileList.push_back(fullPath);}} while (_findnext(handle, &fileInfo) == 0); // 找到下一個文件_findclose(handle); // 關(guān)閉搜索句柄return fileList;
}int main() {string folder = "C:\\MyDocs";string extension = ".txt"; // 可改為.pdf/.jpg等vector<string> files = getFilesByExtension(folder, extension);cout << "找到 " << files.size() << " 個" << extension << "文件:\n";for (const string& path : files) {cout << "? " << path << endl;}return 0;
}