您的位置:首页 > 编程语言 > C语言/C++

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.

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: