The intptr_t type is used to record folder handles, note that the type is not a pointer type, but a redefinition of the int type.
The _finddata_t struct type is used to record file information.
The _finddata_t structure is defined as follows
struct _finddata_t {
unsigned attrib; Store file properties
__time64_t time_create; Store file creation time
__time64_t time_access; The last time the file was accessed
__time64_t Time_write; The time that the file was last modified
_fsize_t size; Size of the storage file
Char name[260]; Store file name
};
_findfirst () function
The _findfirst () function is prototyped as follows:
intptr_t _findfirst (
const char *filespec,//destination file
struct _finddata_t *fileinfo//_finddata_t structure for storing file information
);
If the function succeeds, 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.
_findnext () function
The _findnext () function is prototyped as follows:
int _findnext (
intptr_t handle,//search handle, through _findfirst () function to obtain struct _finddata_t *fileinfo//For storing file information _finddata_t structure);
If the function succeeds, returns 0, otherwise returns-1. If there are no more files to find, it can also cause a failure.
_findclose () function
The prototype is as follows:
int _findclose ( intptr_t handle//search handle);
This function is used to close the search handle
The code is as follows:
void Cdlg::onbnclickedok () {//TODO: In this Add control notification handler code UpdateData (TRUE); m_listfile.resetcontent ();//Wide-byte-to-multibyte char * Ppathbuf = Null;int pathbufsize = WideCharToMultiByte (0, 0, M_szpath.getbuffer (), M_szpath.getlength (), PPathBuf, 0, NULL , NULL), if (pathbufsize <= 0) m_listfile.addstring (_t ("Get Multibyte buffer size error");pP athbuf = new Char[pathbufsize + 1];memset ( Ppathbuf, 0, pathbufsize + 1); WideCharToMultiByte (0, 0, M_szpath.getbuffer (), M_szpath.getlength (), ppathbuf, Pathbufsize + 1, 0, 0); if (strlen ( PPATHBUF) <= 0) m_listfile.addstring (_t ("Wide-byte-to-multibyte error"));queue<string> *pvect = new Queue<string>;if ( Getpathfile (ppathbuf, pvect) = = False) m_listfile.addstring (_t ("Traverse all files in directory failed!")); Else{while (!pvect->empty ()) {string szfilename = Pvect->front (); LPWStr PBuf = Null;int Nlen = MultiByteToWideChar (0, 0, (char*) szfilename.c_str (), Szfilename.length (), PBuf, 0); if (NLen > 0) {pBuf = new Tchar[nlen + 1];memset (pBuf, 0, sizeof (TCHAR) * (Nlen + 1)); MultiByteToWideChar (0, 0, (char*) szfilename.c_str (), Szfilename.length (), PBuf, Nlen); m_listfile.addstring (PBUF);d elete[] pbuf;pbuf = NULL;} Pvect->pop ();}} delete[] Ppathbuf;ppathbuf = NULL; UpdateData (FALSE);} BOOL Cdlg::getpathfile (const char* Ppath, queue<string> *pvect) {if (!ppath | |!ppath) return false;char* SzPath = new Char[128];memset (szpath, 0, +), _snprintf_s (szpath, N., "%s\\*.*", Ppath); intptr_t handle;_finddata_t FindData; Handle = _findfirst (szpath, &finddata); if (Handle = =-1) return False;do {if (strcmp (Finddata.name, ".")! = 0 && ; strcmp (Finddata.name, "...")! = 0) {Pvect->push (finddata.name); if (STRRCHR (Finddata.name, '. ') = = NULL) {string Sz = Ppath;sz + = "\"; sz + = Finddata.name; Getpathfile (Sz.c_str (), pvect);}}} while (_findnext (Handle, &finddata) = = 0) _findclose (Handle);d elete[] Szpath;szpath = Null;return true;}
All files under the C + + traversal path