C ++ get the path folder of all files

Code

getFiles()The role of the function:

path is a path to the folder function pathto find all the files in the folder (including files in subfolders), then the path of all files storedfiles

#include <io.h> //实现功能需要包含头文件io.h
void getFiles(string path, vector<string>& files)
{
    intptr_t   hFile = 0;//文件句柄,过会儿用来查找
    struct _finddata_t fileinfo;//文件信息
    string p;   
    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
     //如果查找到第一个文件
    {
        do
        {
            if ((fileinfo.attrib &  _A_SUBDIR))//如果是文件夹
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
            }
            else//如果是文件
            {
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
            }
        } while (_findnext(hFile, &fileinfo) == 0); //能寻找到其他文件

        _findclose(hFile);  //结束查找,关闭句柄
    }
}

Code Reading

_finddata_t

It is a structure to store a file related information, see its definition:

#ifdef _USE_32BIT_TIME_T
    #define _finddata_t     _finddata32_t
    #define _finddatai64_t  _finddata32i64_t
#else
    #define _finddata_t     _finddata64i32_t
    #define _finddatai64_t  __finddata64_t
#endif

I am here is _finddata64i32_tto see its definition

struct _finddata64i32_t
{
    unsigned    attrib;
    __time64_t  time_create;    // -1 for FAT file systems
    __time64_t  time_access;    // -1 for FAT file systems
    __time64_t  time_write;
    _fsize_t    size;
    char        name[260];
};

attribIs a member of the structure, is an abbreviation attribute (attribute).

It represents the properties of the file, below is appropriate macros

#define _A_NORMAL 0x00 // Normal file - No read/write restrictions
#define _A_RDONLY 0x01 // Read only file
#define _A_HIDDEN 0x02 // Hidden file
#define _A_SYSTEM 0x04 // System file
#define _A_SUBDIR 0x10 // Subdirectory
#define _A_ARCH   0x20 // Archive file

Members nameis the file name ... Well ..

_findfirst

The first parameter is a string marked files, supports wildcards: *.crepresents the suffix .c file, *on behalf of all files

The second parameter is the _finddata_taddress of the variable type. This variable is used to save the file information.

This pit

I was a little bit different with the Internet, online I see examples of definitions hFileare longtype I use longto open the file type on the problem.

In fact, VS has warned warning C4244: “=”: 从“intptr_t”转换到“long”,可能丢失数据.

I did not care about it, then get rid of the types of success. (On the red this point, VS best in the world!)

View _findfirstdefinitions

#ifdef _USE_32BIT_TIME_T
    #define _findfirst      _findfirst32
    #define _findnext       _findnext32
    #define _findfirsti64   _findfirst32i64
    #define _findnexti64     _findnext32i64
#else
    #define _findfirst      _findfirst64i32
    #define _findnext       _findnext64i32
    #define _findfirsti64   _findfirst64
    #define _findnexti64    _findnext64
#endif

I am here is _findfirst64i32to see its definition

_ACRTIMP intptr_t __cdecl _findfirst64i32(
        _In_z_ char const*              _FileName,
        _Out_  struct _finddata64i32_t* _FindData
        );

Description Function Returnsintptr_t

Continue to view intptr_tthe definition, get

#ifdef _WIN64
    typedef unsigned __int64 size_t;
    typedef __int64          ptrdiff_t;
    typedef __int64          intptr_t;
#else
    typedef unsigned int     size_t;
    typedef int              ptrdiff_t;
    typedef int              intptr_t;

So intptr_ton my computer is actually __int64converted into long data may be lost.

_findnext

View definition

#ifdef _USE_32BIT_TIME_T
    #define _findfirst      _findfirst32
    #define _findnext       _findnext32
    #define _findfirsti64   _findfirst32i64
    #define _findnexti64     _findnext32i64
#else
    #define _findfirst      _findfirst64i32
    #define _findnext       _findnext64i32
    #define _findfirsti64   _findfirst64
    #define _findnexti64    _findnext64
#endif

I am here is _findnext64i32to see its definition

   _ACRTIMP int __cdecl _findnext64i32(
        _In_  intptr_t                 _FindHandle,
        _Out_ struct _finddata64i32_t* _FindData
        );

Nothing to talk about, take a look at _findfirstpart to understand this anymore.

Bit computing

fileinfo.attrib & _A_SUBDIR, The code uses the bitwise AND &. In this representation is the folder (subdirectory). Bitwise often used to mean properties. The first concrete do not speak.

Author: @ smelly salted fish

This article original author, please indicate the source: https://chouxianyu.github.io

Forwarding and comments are welcome!

Guess you like

Origin www.cnblogs.com/chouxianyu/p/11270030.html