In an earlier article we talked about traversing all the files in a directory with the Windows API, and this time we talked about using a windows/linux generic method to traverse all the files in a directory.
The Windows/linux IDE will provide a header file--<io.h>. Look at the name, it seems to be about I/O, but in fact it provides the ability to find files similar to Win32_find_data, FindFirstFile (), FindNextFile (), and FindClose ().
_finddata_t structure
The _finddata_t structure is used to record information about the found file. In fact there are _finddata32_t, _finddata32i64_t, _finddata64i32_t, _finddata64_t, _wfinddata32_t, _wfinddata32i64_t, _ wfinddata64i32_t, _wfinddata64_t eight structures, but all only differ on 32-bit/64-bit integers and character types, but they are the same on a whole. Broadly defined as follows (MSDN):
struct _finddata_t{ unsigned attrib; size_t Time_create; size_t time_access; size_t Time_write; _fsize_t size; Char Name[_max_path];};
For different _finddata_t structures, the type _time32_t or _time64_t,size of Time_create, time_access, and Time_write is _fsize_t or __int64, Name is Char[_max_path] or Wchar_t[_max_path].
attrib
Unsigned type, file attribute.
Time_create
_time32_t/_time64_t type, file creation time (FAT file system is-1). stored in UTC format, if required to convert to local time, use localtime_s ().
Time_access
_time32_t/_time64_t type, the last time the file was accessed (the FAT file system is-1). stored in UTC format, if required to convert to local time, use localtime_s ().
Time_write
_time32_t/_time64_t type, the time the file was last written to. stored in UTC format, if required to convert to local time, use localtime_s ().
Size
The _fsize_t/__int64 type, the length of the file, in bytes.
Name
Char[_max_path]/wchar_t[_max_path] Type, File/directory name, not including path.
For file systems that do not support file creation time and the last access time of the file, Time_create and time_access are-1.
_MAX_PATH is defined as 260 in stdlib.h.
The general _finddata_t is defined as _finddata32_t/_finddata64i32_t,_wfinddata_t is defined as _wfinddata32_t/_wfinddata64i32_t. for convenience, _finddata_t and _wfinddata_t are collectively referred to as _finddata_t.
File Property Constants
A file/directory can have multiple properties, each of which can be one of the attributes listed below.
_a_arch
File. The file is set when it is changed or cleared by the backup command. Value: 0x20.
_a_hidden
Hide. Using the DIR directive is generally not visible unless you use the/ah option. Value: 0x02.
_a_normal
Ordinary. The file has no more properties to be set and can be read or written without restriction. Value: 0x00.
_a_rdonly
Read-only. You cannot open the file for "write" purposes, and you cannot create a file with the same name. Value: 0x01.
_a_subdir
Sub-directories. Value: 0x10.
_a_system
System files. It is generally not visible with the DIR directive unless you use the/A or/a:s option. Value: 0x04.
To check if x contains an attribute A, you can check with X & A. Specifying multiple properties can use the bitwise OR operator, such as _a_system | _a_rdonly | _a_hidden.
wildcard character (wildcards)
You need to use wildcards when traversing file directories, see <psg>.
_findfirst ()/_findnext ()/_findclose () function
_findfirst () function
intptr_t _findfirst ( constChar * filespec, struct _finddata_t *FileInfo);
In fact, there are 10 versions of _findfirst (), only one of them is listed here.
Filespec
const char */const wchar_t * Type, Target file description (can contain wildcard characters).
FileInfo
_finddata_t * Type, the function will fill in the File/directory information.
return value
If successful, returns a unique search handle identifying one or a set of files that match the filespec description, which can be used for the next _findnext () and _findclose () functions. otherwise _findfirst () returns-1. Note that intptr_t is not a pointer type, just a typedef of int or __int64.
_findnext () function
int _findnext ( intptr_t handle, struct _finddata_t *fileinfo);
Handle
intptr_t type, search handle.
FileInfo
_finddata_t * Type, the function will fill in the File/directory information.
return value
If successful, returns 0, otherwise returns-1. If there are no more files to find, it can also cause a failure.
Program code
All Files (windows/linux, super-verbose) in C + + traversal directory