How to implement C++ programming to find and traverse files using findfirst and findnext
This article mainly introduces "how to achieve C++ programming using findfirst and findnext to find and traverse files". In daily operation, I believe many people have doubts about how to achieve C++ programming to find and traverse files using findfirst and findnext. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve C++ programming using findfirst and findnext to find and traverse files". Next, please follow the editor to study!
Catalogue
First, take a look at a file structure:
Use _ findfirst and _ findnext to find files
Both of these functions are in io.h.
First, take a look at a file structure: struct _ finddata_t {unsigned attrib; time_t time_create; time_t time_access; time_t time_write; _ fsize_t size; char name;}
Time_t is actually long.
And _ fsize_t, which is unsigned long.
Now let's explain the data members of the structure.
Attrib, which is the attribute of the file you are looking for:
_ A_ARCH (archived), _ A_HIDDEN (hidden), _ A_NORMAL (normal),
_ A_RDONLY (read-only), _ A_SUBDIR (folder), _ A_SYSTEM (system).
Time_create, time_access, and time_write are the time the file was created, the last time the file was accessed, and the last time the file was modified.
Size: file siz
Name: file name.
Use _ findfirst and _ findnext to find files
1. _ findfirst function: long _ findfirst (const char *, struct _ finddata_t *)
The first parameter is the file name, and you can use "*. *" to find all files, or "* .cpp" to find .cpp files. The second parameter is the _ finddata_t structure pointer. If the search is successful, the file handle is returned; if it fails,-1 is returned.
2. _ findnext function: int _ findnext (long, struct _ finddata_t *)
The first parameter is the file handle, and the second parameter is also the _ finddata_t structure pointer. If the lookup is successful, it returns 0, and if it fails, it returns-1.
3. _ findclose () function: int _ findclose (long)
There is only one parameter, the file handle. If the shutdown successfully returns 0, the failure returns-1.
# include # include # include using namespace std;bool transfer (string fileName, int exeNum); void dfsFolder (string folderPath, ofstream & fout); int main () {_ finddata_t file; int k; long HANDLE; k = HANDLE = _ findfirst ("*. *", & file); while (k! =-1) {cout