This article can be reproduced, reproduced please indicate the source: http://www.cnblogs.com/collectionne/p/6815924.html.
In the previous article we talked about using the Windows API to traverse all the files in a directory, and this time we talked about using a Windows/linux method to traverse all the files in a directory.
The Windows/linux IDE will provide a header file--<io.h>. Looking at the name, it seems to be about I/O, but in fact it also 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 files found. In fact there are _finddata32_t, _finddata32i64_t, _finddata64i32_t, _finddata64_t, _wfinddata32_t, _wfinddata32i64_t, _ wfinddata64i32_t, _wfinddata64_t eight structures, but all differ only in 32-bit/64-bit integers and character types, but are the same on the 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 types of time_create, time_access, and Time_write are _time32_t or _time64_t,size of type _fsize_t or __int64, Name is Char[_max_path] or Wchar_t[_max_path].
attrib
Unsigned type, file attributes.
time_create
_time32_t/_time64_t type, file creation time (FAT file system is-1). stored in UTC format, use localtime_s () if necessary to convert to local time.
time_access
_time32_t/_time64_t type, the last time the file was accessed (the FAT file system is-1). stored in UTC format, use localtime_s () if necessary to convert to local time.
Time_write
_time32_t/_time64_t type, the last time the file was written. stored in UTC format, use localtime_s () if necessary to convert to local time.
size
_fsize_t/__int64 type, the length of the file in bytes.
name
Char[_max_path]/wchar_t[_max_path] Type, File/directory name, no path included.
For file systems that do not support file creation time, file last access time, Time_create and time_access are-1.
_MAX_PATH is defined as 260 in stdlib.h.
The general _finddata_t is defined as the _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 below.
file Attribute Constants
A file/directory can have multiple properties, each of which can be one of the properties listed below.
_a_arch
File. The file is set when it is changed or cleared by the backup command. Value: 0x20.
_a_hidden
Hide. The dir instruction is generally not visible unless the/ah option is used. Value: 0x02.
_a_normal
Ordinary. File no more attributes are set, can be read or written without restrictions. 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
Subdirectories. Value: 0x10.
_a_system
System files. Using the DIR directive is generally invisible unless you use the/A or/a:s option. Value: 0x04.
To check if X contains a property A, you can check with X & A. You can specify multiple properties by using the bitwise OR operator, such as _a_system | _a_rdonly | _a_hidden.
wildcard character (wildcards)
You need to use wildcard characters to traverse the file directory, as described in my other article.
_findfirst ()/_findnext ()/_findclose () function
_findfirst () function
intptr_t _findfirst (
const char * filespec,
struct _finddata_t *fileinfo
);
There are actually 10 versions of _findfirst (), and only one 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 that identifies one or a set of files that match the filespec description, and can be used for the next _findnext () and _findclose () functions. otherwise _findfirst () returns-1. Note that intptr_t is not a pointer type, but 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, return 0, otherwise return-1. If there are no more files to find, it can also cause failure.
_findclose () function
int _findclose (
intptr_t handle
);
Closes the search handle and releases the appropriate resource.
Handle
The search handle.
return value
Successfully returns 0, failure returns-1.
Program Code
1. Traverse all files in the directory
#include <iostream> #include <cstring>/For strcat () #include <io.h> using namespace std;
void Listfiles (const char * dir);
int main () {char dir[200];
cout << "Enter a directory (ends with \ ' \\\ '):";
Cin.getline (dir, 200); strcat (dir, "*.*");
Add the wildcard character listfiles (dir) after the directory to be traversed;
return 0;
} void Listfiles (const char * dir) {intptr_t handle;
_finddata_t FindData; Handle = _findfirst (dir, &finddata);
Look for the first file in the directory if (handle = = 1) {cout << "Failed to find" file!\n;
Return
} do {if (Finddata.attrib & _a_subdir && strcmp (Finddata.name, ".") = 0 && strcmp (Finddata.name, "...") = = 0)//is a subdirectory and is not "."
or ".."
cout << finddata.name << "\t<dir>\n";
else cout << finddata.name << "\ t" << finddata.size << Endl; } whilE (_findnext (handle, &finddata) = = 0);
Find the next file in the directory cout << "done!\n"; _findclose (handle); Close Search Handle}
The program traverses all files/directories in the directory and outputs the file size if it is a file.
Note that the _findnext () function returns 0 successfully, so you should add ==0 or!=-1 to make a judgment and not omit it.
In addition, there is a noteworthy place:
if (Finddata.attrib & _a_subdir
&& strcmp (Finddata.name, ".")
&& strcmp (Finddata.name, "...")
)
...
When searching using _findfirst (), _findnext (), you may get a "." and ".." Two folder names. These two values can be ignored.
2. Traverse all files in the directory
Note "in directory" instead of "directory", this program will traverse all the files contained in a directory.
#include <iostream> #include <cstring>/For strcpy (), strcat () #include <io.h> using namespace
Std
void Listfiles (const char * dir);
int main () {char dir[200];
cout << "Enter a directory:";
Cin.getline (dir, 200);
Listfiles (dir);
return 0;
} void Listfiles (const char * dir) {char dirnew[200];
strcpy (Dirnew, dir); strcat (dirnew, "\\*.*");
The first search intptr_t handle after adding "\\*.*" to the table of contents;
_finddata_t FindData;
Handle = _findfirst (dirnew, &finddata);
if (handle = = 1)//check whether successful return; do {if (Finddata.attrib & _a_subdir) {if strcmp (Finddata.name, ".") = = 0 | | strcmp ( Finddata.name, "..")
= = 0) continue;
cout << finddata.name << "\t<dir>\n";
Next Search strcpy (Dirnew, dir) by adding "\" and the search directory name behind the directory;
strcat (dirnew, "\ \");
strcat (Dirnew, finddata.name); Listfiles (dirnew);
else cout << finddata.name << "\ t" << finddata.size << "bytes.\n";
while (_findnext (handle, &finddata) = = 0); _findclose (handle); Close Search Handle}