A method for obtaining folder content in Windows and Linux

Source: Internet
Author: User
Tags lstat

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.