Windows:
Header files: <Io. h> and <direct. h>
Key functions: _ findfirst and _ findnext
Key struct: _ finddata_t
_ Finddata_tfileinfo; intptr_t hfile; string root; root. Assign (PATH); // folder absolute path int Len = root. Length (); If (! Isendobliqueline (PATH) // the end of the path is not '\' {root. append ("\");} root. append ("*"); // all files in the path hfile = _ findfirst (root. c_str (), & fileinfo); If (-1 = hfile) {assert (false); return;} strcpy (m_currentpath, PATH ); // save it to m_currentpath // clear the previous path information filespathvector. clear (); // custom container do for saving information // save information {listpai_info tmpdirinfo = {0}; transfileinfo (fileinfo, tmpdirinfo); filespathvector. push_back (tmpdirinfo);} while (_ findnext (hfile, & fileinfo) = 0 );
Linux:
Header files: <dirent. h>, <sys/types. h>, and <sys/STAT. h>
Key functions: readdir and lstat
Key struct: dirent
Dir * dir; If (! (DIR = opendir (PATH) {assert (false); return ;}Strcpy (m_currentpath, PATH); // save it to m_currentpathstruct dirent * d_ent; char fullpath [128]; filespathvector. Clear (); While (d_ent = readdir (DIR ))! = NULL) {struct stat file_stat; // If (strncmp (d_ent-> d_name ,". ", 1) = 0) // {// continue; // ignore ". "directory //} memset (fullpath, '\ 0', sizeof (fullpath); strcpy (fullpath, PATH); If (! Strcmp (fullpath, "/") {fullpath [0] = '\ 0';} strcat (fullpath, "/"); strcat (fullpath, d_ent-> d_name ); if (lstat (fullpath, & file_stat) <0) {assert (false); return;} // Save the information to your own data structure, save the name list1__info tmpdirinfo = {0}; strcpy (tmpdirinfo. cfilename, d_ent-> d_name); transfileinfo (& file_stat, tmpdirinfo); filespathvector. push_back (tmpdirinfo);} closedir (DIR );
It is worth noting that in this way, the path separation in Windows is '\', while in Linux '/', it is inconvenient to use it.
if you have other better methods, please contact us!