C++ USES _findfirst and _findnext to find files
- 2020-06-07 04:53:21
- OfStack
C++ file lookup
How do we find files in C++? We need a structure and some functions that you might not be familiar with. These functions and structures are in the header file struct _finddata_t and the functions _findfirst, _findnext, and _fineclose. How to use it in detail, let's take a look at it
_findfirst and _findnext find files
1. Both of these functions are in ES17en.h.
2. First, understand the following file structure:
struct _finddata_t {
unsigned attrib;
time_t time_create;
time_t time_access;
time_t time_write;
_fsize_t size;
char name[260];
};
time_t, which is long
And _fsize_t is unsigned long
Now let's explain the data members of the structure under 1.
attrib, the properties of the file being sought: _A_ARCH (archive), _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 when the file was created, the last time the file was accessed, and the last time the file was modified, respectively.
size: File size
name: Filename.
3. Find the file with _findfirst and _findnext
1. _findfirst function:
long _findfirst(const char *, struct _finddata_t *);
The first parameter is the file name. You can find all files with "*.*" or you can find.cpp files with "*.cpp ". The second parameter is the _finddata_t struct pointer. Returns the file handle on success or -1 on failure.
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 struct pointer. Returns 0 on success, -1 on failure.
3. _findclose() function:
int _findclose(long);
Only 1 argument, file handle. Returns 0 on close success, -1 on failure.
#include <io.h>
#include <iostream>
#include <fstream>
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 << file.name << endl;
k = _findnext(HANDLE, &file);
}
_findclose(HANDLE);
transfer("C:\\Windows\\*.exe", 0);
ofstream o_fstream;
dfsFolder("E:\\\WHU\\Study", o_fstream);
return 0;
}
//_findfirst The function returns a handle that matches to a file, of data type long .
// The traversal procedure can specify the file type, which passes through FileName For example, to traverse C : \WINDOWS Of all the .exe file
bool transfer(string fileName , int exeNum)
{
_finddata_t fileInfo;
long handle = _findfirst(fileName.c_str(), &fileInfo);
if (handle == -1L)
{
cerr << "failed to transfer files" << endl;
return false;
}
do
{
exeNum++;
cout << fileInfo.name << endl;
} while (_findnext(handle, &fileInfo) == 0);
cout << " .exe files' number: " << exeNum << endl;
return true;
}
// Iterate over all the files under the folder and its subfolders. The folder directory in the operating system is a tree structure that USES a deep search strategy to traverse all files. use _A_SUBDIR attribute
// Check whether there are subdirectories if In a branch, because the system is coming in 1 When subdirectories are matched to the first two files ( clip ) is "."( In the current directory ) . ".."( on 1 Layer directory ) .
// We need to ignore both cases. When you need to do something with a traversal to a file else Just add the appropriate code to the branch
void dfsFolder(string folderPath, ofstream &fout)
{
_finddata_t FileInfo;
string strfind = folderPath + "\\*";
long Handle = _findfirst(strfind.c_str(), &FileInfo);
if (Handle == -1L)
{
cerr << "can not match the folder path" << endl;
exit(-1);
}
do{
// Determine if there is a subdirectory
if (FileInfo.attrib & _A_SUBDIR)
{
// This is an important statement
if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0))
{
string newPath = folderPath + "\\" + FileInfo.name;
dfsFolder(newPath, fout);
}
}
else
{
fout<<folderPath.c_str() << "\\" << FileInfo.name << " ";
cout << folderPath.c_str() << "\\" << FileInfo.name << endl;
}
} while (_findnext(Handle, &FileInfo) == 0);
_findclose(Handle);
fout.close();
}
//#include <iostream>
//#include <string>
//#include <io.h>
//using namespace std;
//
//int main()
//{
// _finddata_t file;
// long longf;
// string tempName;
// //_findfirst Returns the long type ; long __cdecl _findfirst(const char *, struct _finddata_t *)
// if ((longf = _findfirst("E:\\WHU\\Study\\*.*", &file)) == -1l)
// {
// cout << " File not found !\n";
// return 0;
// }
// do
// {
// cout << " File list :\n";
// tempName = file.name;
// if (tempName[0] == '.')
// continue;
// cout << file.name<<endl;
//
// if (file.attrib == _A_NORMAL)
// {
// cout << " Common file ";
// }
// else if (file.attrib == _A_RDONLY)
// {
// cout << " A read-only file ";
// }
// else if (file.attrib == _A_HIDDEN)
// {
// cout << " Hidden files ";
// }
// else if (file.attrib == _A_SYSTEM)
// {
// cout << " System files ";
// }
// else if (file.attrib == _A_SUBDIR)
// {
// cout << " subdirectories ";
// }
// else
// {
// cout << " Archive file ";
// }
// cout << endl;
// } while (_findnext(longf, &file) == 0);//int __cdecl _findnext(long, struct _finddata_t *); Returns the name of the next file if found successfully 0, Otherwise returns -1
//
// _findclose(longf);
//
// return 0;
//}
conclusion