C + + Get all the file names under the folder three ways

Source: Internet
Author: User

1

Use the DOS command to save all file names in the folder to the specified file, then read from the file and store in the vector

         String imglist_file = "Test_result\\imglist.txt";//Store file name list
	string temp_imgname;
	String Dir_command ("dir");
	Dir_command + = M_strimageset + "/b >" + imglist_file;//m_strimageset is the variable that I built before I stored the folder path
	ifstream file (imglist_ File.c_str (), ios::in);
	System (DIR_COMMAND.C_STR ());
	Vector <string> imglist;

	The image name is stored in vector while
           (file>>temp_imgname)
	{
	     imglist.pushback (temp_imgname);
	}
	File.close ();
		

2

Use _findfirst, _findnext to get all file names

         string Temp_imgname;
	Long   hfile   =   0;//file handle
	_finddata_t fileinfo;//file information
          vector<string> imglist	
	if (hfile = _findfirst (m_strimageset+ "\\*", &fileinfo))!=  -1)//m_strimageset is the variable I created earlier. Folder path
	{do
		{if
			((Fileinfo.attrib &  _a_subdir))
			{
				continue;//if it is a directory skip
			}
			else
			{
				Imglist.push_back (temp_imgname);
			}
		while (_findnext (hfile, &fileinfo)  = = 0);
	
		_findclose (hfile);
	}


3 Using Finder to get

         Win32_find_data Findfiledata;
	int finder = 1;
	CString m_strimageset_t = m_strimageset + "\\*.*";)//m_strimageset is a variable that I built before. The folder path
	CString m_strimageset_t1 = M_strimageset + "\";
         string imgname_temp;
         Vector <string> imglist;
 	HANDLE hfind = FindFirstFile (m_strimageset_t, &findfiledata);
	if (hfind = = Invalid_handle_value)
	{
		printf ("INVALID File HANDLE. GetLastError reports%d\n ", GetLastError ());
		return;
	}
	else
	{while  
		(Finder)
		{

			if (_tcscmp (findfiledata.cfilename,_t (".")) ==0) | | (_TCSCMP (findfiledata.cfilename,_t ("..")) ==0)//filter The default two folders
			in the explorer. {
				finder = FindNextFile (hfind,&findfiledata);  
				Continue;
			}
			else
                           {
				temp_imgname = findfiledata.cfilename; 
				Imglist.pushback (temp_imgname);
	}} FindClose (hfind); 


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.