Windows 用c++实现,文件路径和目录名的读取保存
2017-03-21 20:02
751 查看
要实现如题目所说的功能,只需要理解递归和C++的基本操作就可以实现了。
- 需要微软的Filename Search Functions
- Remarks
-The _findfirst function provides information about the first instance of a file name that matches the file specified in the filespec argument. You can use in filespec any combination of wildcard characters that is supported by the host operating system.
unsigned attrib
File attribute.
time_t time_create
Time of file creation (–1L for FAT file systems). This time is stored in UTC format. To convert to the local time, use localtime_s.
time_t time_access
Time of the last file access (–1L for FAT file systems). This time is stored in UTC format. To convert to the local time, use localtime_s.
time_t time_write
Time of the last write to file. This time is stored in UTC format. To convert to the local time, use localtime_s.
_fsize_t size
Length of the file in bytes.
char name[ _MAX_PATH]
Null-terminated name of matched file or directory, without the path.
- 需要微软的Filename Search Functions
- Remarks
-The _findfirst function provides information about the first instance of a file name that matches the file specified in the filespec argument. You can use in filespec any combination of wildcard characters that is supported by the host operating system.
The functions return file information in a _finddata_t structure, which is defined in IO.h. Various functions in the family use many variations on the *_finddata_t* structure. The basic *_finddata_t* structure includes the following elements:
unsigned attrib
File attribute.
time_t time_create
Time of file creation (–1L for FAT file systems). This time is stored in UTC format. To convert to the local time, use localtime_s.
time_t time_access
Time of the last file access (–1L for FAT file systems). This time is stored in UTC format. To convert to the local time, use localtime_s.
time_t time_write
Time of the last write to file. This time is stored in UTC format. To convert to the local time, use localtime_s.
_fsize_t size
Length of the file in bytes.
char name[ _MAX_PATH]
Null-terminated name of matched file or directory, without the path.
头文件
//获取完整的文件名 void getAllFilesPath(string path, vector<string>& filesName); //只获取在目录下的所有文件名 void getAllFilesName(string path, vector<string>& filesName); //用文件的读取方式 void writeFilesName(const vector<string>& filesName);
#include <iostream> #include <algorithm> #include <io.h> #include <fstream> #include <vector> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; void getAllFilesPath(string path, vector<string>& filesName) { //文件句柄 long hFile = 0; //文件信息 struct _finddata_t fileinfo; //文件搜索指针 string tempFilePath; //可以对要查找的目录或者文件名进行限制查找。 //比如,只要后缀是.txt的文件名,则可以这样设置 //tempFilePath.assign(path).append("\\*.txt").c_str() //当然,其查找到的结果只能是当前目录下的所有.txt文件。 if ((hFile = _findfirst(tempFilePath.assign(path).append("\\*").c_str(), &fileinfo)) == -1) { cout << "Not Find File!" << endl; exit(-1); } else { do { tempFilePath = path + "\\" + fileinfo.name; //比较文件类型是否是文件夹 if ((fileinfo.attrib & _A_SUBDIR)) { //是否是特殊的文件(.,..),etc. if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) { filesName.push_back(tempFilePath); getAllFilesPath(tempFilePath, filesName); } } else { filesName.push_back(tempFilePath); } }while(_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } } void getAllFilesName(string path, vector<string>& filesName){ long hFile = 0; struct _finddata_t fileInfor; string p; if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileInfor)) == -1) { cout <<"Not Find File!" << endl; exit(-1); } else { do { if ((fileInfor.attrib & _A_SUBDIR)) { if (strcmp(fileInfor.name, ".") != 0 && strcmp(fileInfor.name, "..") != 0) getAllFilesName(p.assign(path).append("\\").append(fileInfor.name), filesName); } else { filesName.push_back(fileInfor.name); } }while(_findnext(hFile, &fileInfor) == 0); } _findclose(hFile); } void print(const vector<string>& filesName) { cout << "----------------------------------------\n"; cout << "| |\n"; cout << "| ---File list--- |\n"; cout << "| ---count: " << filesName.size() << "--- |\n"; cout << "----------------------------------------\n"; vector<string>::const_iterator iter; for (iter = filesName.begin(); iter != filesName.end();++iter) { cout << *iter << endl; } cout << "-----------------END--------------------\n"; } void writeFilesName(const vector<string>& filesName) { char *f = "fileName.txt"; ofstream ofs(f); vector<string>::const_iterator iter; for (iter = filesName.begin(); iter != filesName.end(); ++iter) { ofs << *iter << endl; } ofs.close(); } int main(int argc, char **argv) { //将查找的结果写入fileName.txt // freopen("fileName.txt", "w", stdout); vector<string> filesName; string path = "E:\\acm\\ggg"; // getAllFilesName(path, filesName); getAllFilesPath(path, filesName); writeFilesName(filesName); //输入文件名 // print(filesName); return 0; }
相关文章推荐
- windows下,c /c++实现磁盘扫描,结合配置文件,读取特定后缀文件目录代码
- windows下,c /c++实现磁盘扫描,结合配置文件,读取特定后缀文件目录代码
- windows下,c /c++实现磁盘扫描,结合配置文件,读取特定后缀文件目录代码
- C++读取特定路径下文件目录及文件名称
- C++读取与保存bmp图片文件编程实现
- windows C++ 遍历目录,获取文件名和文件路径
- C++读取特定路径下文件目录及文件名称
- C++读取特定路径下文件目录及文件名称
- windows 与linux 下用C++读取sqlite实现文件复制(一)
- 转载:C++读取特定路径下文件目录及文件名称
- C++读取、旋转和保存bmp图像文件编程实现
- Python实现读取目录所有文件的文件名并保存到txt文件代码
- C++读取特定路径下文件目录及文件名称&&文件结构
- Python实现读取目录所有文件的文件名并保存到txt文件代码
- C++读取、旋转和保存bmp图像文件编程实现
- Python实现读取目录所有文件的文件名并保存到txt文件代码
- Python实现读取目录所有文件的文件名并保存到txt文件代码
- windows 与linux 下用C++读取sqlite实现文件复制(二)
- C++读取、旋转和保存bmp图像文件编程实现
- windows C++ 遍历目录,获取文件名和文件路径