Windows 用c++实现,文件路径和目录名的读取保存

要实现如题目所说的功能,只需要理解递归和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;
}

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值