C++ to read a specific path under the folder and filename method

  • 2020-04-02 02:25:41
  • OfStack

The example code described in this article mainly implements the function of reading all folder names or all suffixed file names under a given path. Specific solutions are as follows:
 
The main use of the following header files (classes) : IO. H, fstream, string.
 
First, read the names of all folders and files in a given path with the full path. The implementation code is as follows:


 void getAllFiles( string path, vector<string>& files) 
 { 
   //File handle
   long  hFile  =  0; 
   //File information
   struct _finddata_t fileinfo; 
   string p; 
   if((hFile = _findfirst(p.assign(path).append("\*").c_str(),&fileinfo)) != -1) 
   { 
     do 
     {  
       if((fileinfo.attrib & _A_SUBDIR)) 
       { 
         if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0) 
         {
          files.push_back(p.assign(path).append("\").append(fileinfo.name) );
           getFilesall( p.assign(path).append("\").append(fileinfo.name), files ); 
         }
       } 
       else 
       { 
         files.push_back(p.assign(path).append("\").append(fileinfo.name) ); 
       } 
     }while(_findnext(hFile, &fileinfo) == 0); 
     _findclose(hFile); 
   } 
 } 

The function takes two arguments, the first of which is a path string (of type string, preferably an absolute path). The second parameter stores variables for the folder and file names (vector type, passed by reference).
Call the format in the main function (and save the results in the file "allfiles.txt", the first total) :


 char * filePath = "E:\YunShi"; 
 vector<string> files; 
 char * distAll = "AllFiles.txt";
 getFilesall(filePath, files);
 ofstream ofn(distAll);
 int size = files.size(); 
 ofn<<size<<endl;
 for (int i = 0;i<size;i++) 
 { 
   ofn<<files[i]<<endl; 
 }
 ofn.close();

Similarly, only the current folder name under a given path is read (the following is similar, only the function is given, the call case is the same as above) :


void getJustCurrentDir( string path, vector<string>& files) 
 { 
   //File handle
   long  hFile  =  0; 
  //File information
   struct _finddata_t fileinfo; 
   string p; 
   if((hFile = _findfirst(p.assign(path).append("\*").c_str(),&fileinfo)) != -1) 
   { 
     do 
     {  
       if((fileinfo.attrib & _A_SUBDIR)) 
       { 
         if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0) 
         {
           files.push_back(fileinfo.name);
           //files.push_back(p.assign(path).append("\").append(fileinfo.name) );
         }
           
       }  
     }while(_findnext(hFile, &fileinfo) == 0); 
     _findclose(hFile); 
   } 
 } 

Read only the current file name under a given path:


 void getJustCurrentFile( string path, vector<string>& files) 
 { 
   //File handle
   long  hFile  =  0; 
   //File information
   struct _finddata_t fileinfo; 
   string p; 
   if((hFile = _findfirst(p.assign(path).append("\*").c_str(),&fileinfo)) != -1) 
   { 
     do 
     {  
       if((fileinfo.attrib & _A_SUBDIR)) 
       { 
         ;
       } 
       else 
       { 
         files.push_back(fileinfo.name);
         //files.push_back(p.assign(path).append("\").append(fileinfo.name) ); 
       }  
     }while(_findnext(hFile, &fileinfo) == 0); 
     _findclose(hFile); 
   } 
 } 

Read only all file names under a given path (that is, files containing the current directory and subdirectories) :


void getFilesAll( string path, vector<string>& files) 
 { 
   //File handle
   long  hFile  =  0; 
   //File information
   struct _finddata_t fileinfo; 
   string p; 
   if((hFile = _findfirst(p.assign(path).append("\*").c_str(),&fileinfo)) != -1) 
   { 
     do 
     {  
       if((fileinfo.attrib & _A_SUBDIR)) 
       { 
         if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0) 
         {
           //files.push_back(p.assign(path).append("\").append(fileinfo.name) );
           getFilesA( p.assign(path).append("\").append(fileinfo.name), files ); 
         }
       } 
       else 
       { 
         files.push_back(p.assign(path).append("\").append(fileinfo.name) ); 
       } 
     }while(_findnext(hFile, &fileinfo) == 0); 
     _findclose(hFile); 
   } 
 } 

Related articles: