Get folder under C ++ C ++ get all the file names of all files under the folder name

 

Reprinted: C ++ folder get all the file names

Find the file needs a structure and a few functions. Structure is a struct _finddata_t , function _findfirst , FindNext and _findclose .

struct _finddata_t

This structure is used to store a variety of information files. It is defined as follows:

struct _finddata_t
{
unsigned attrib;
time_t time_create;
time_t time_access;
time_t time_write;
_fsize_t size;
char name[_MAX_FNAME];
};

The meaning of various member variables are as follows:

unsigned attrib

Storage location of the file attributes. It is an unsigned storage unit, an attribute file indicates. File attributes are represented by bit, there are some of the following:


_A_ARCH (Archive)
_A_HIDDEN (Hidden)
_A_NORMAL (normal)
_A_RDONLY (Read Only)
_A_SUBDIR (folder)
_A_SYSTEM (system)

These are defined in the header file macro can be used directly, and the meaning itself is actually an unsigned integer (but this should be an integer power of 2 several times to ensure that only one is 1, and the other bit is 0). Since it is a bit representation, then when a file has multiple attributes, it is often by way or a bit to get integrated several properties. Such as read-only attribute + hidden + system, it should be:

_A_HIDDEN | _A_RDONLY | _A_SYSTEM

time_t time_create:

time_t here is a variable type (long integer? equivalent to long int?), used to store the time, we do not care, just know that this time_create variable is used to store the file creation time on it.

time_t time_access

The last file to be accessed time.

time_t time_write

The file was last modified.

_fsize_t size

Size of the file. _fsize_t here should be the equivalent of unsigned integer representing the number of bytes of the file.

name char [_MAX_FNAME] : filename of the file. _MAX_FNAME macro here is a constant, which is defined in the header file, it represents the maximum length of the file name.

 

Also said earlier, this structure is used to store file information, then the information on how to file a hard disk file "to keep" the memory space represented by this structure go? It is up to _findfirst, _findnext and _fineclose three functions with use.

The following introduced one by one.

long findfirst(char* filespec,struct _finddata_t* fileinfo);


 

Return value : If the search is successful, it will return a long look with a unique type of handle (that is, a unique number). This handle will be used _findnext function. If fails, it returns -1.

Parameters :

  filespec : string marked files, supports wildcards. For example:. * C, then all suffixes current folder for the file C. For example: D: \\ test \\ *, then all the D drive test files within the folder.

  FileInfo : this is used to store a pointer file structure information. This structure must be declared before calling this function, but do not initialize, as long as the memory space can be allocated. After the function is successful, the function will find files into this structure.


 

 

int _findnext( long handle, struct _finddata_t *fileinfo );


 

Return value : If successful return 0, otherwise -1.

Parameters :

  handle : i.e. returned by the function _findfirst back handle.

  FileInfo : pointer file structure information. After the file is found, the function of this information into the file structure.


 

int _findclose( long handle );


 

Return Value : returns 0 on success, -1 failure.

Parameters :

  handle : _findfirst function returns a handle to come back.


 

 

See here, estimated that about one can guess, right? Find _findfirst first with the first file, if successful return with a handle calls _findnext function to find other documents, when the look is completed by the end of _findclose function lookup. Here we follow this idea to write a file sam find all files in the project directory folder and output file name.

/ * 
@Author: CodingMengmeng 
@theme: Get all file names specified folder 
@time: 2017-1-13 11:46:22 
@blog: http://www.cnblogs.com/codingmengmeng/ 
* / 
#include <the io.h>   
#include <the iostream>   
#include <Vector>  
 the using  namespace STD; 

void getFiles ( String path, Vector < String > & files) 
{ 
    // file handle   
    Long    hFile = 0 ;
     // file information, declare a storage structure information file   
    struct _finddata_t FileInfo;
     string P; // string storage path
    IF ((hFile = _findfirst (p.assign (path) .append ( " \\ * " ) .c_str (), & FileInfo)) = -! 1 ) // If the lookup is successful, the 
    {
         do 
        { 
            // If it is directory, iteration of (ie, there is a folder within a folder)   
            IF ((fileinfo.attrib &   _A_SUBDIR)) 
            { 
                // file name is not equal. "" && file name is not equal to ".."
                 // Indicates current directory
                 / / .. parent directory represents the current directory
                 // when the judge and both will be ignored, otherwise they jump out of the infinite recursion! 
                IF (strcmp (fileinfo.name, " . " )! = 0 && strcmp (fileinfo.name,") != 0)
                    getFiles(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函数结束查找
        _findclose(hFile);
    }
}


int main(){
    char * filePath = "sam" ; // set up their own directory   
    Vector < String > Files; 

    /// / overview of all files under the path   
    getFiles (filePath, Files); 

    char STR [ 30 ];
     int size = files.size ();
     for ( int I = 0 ; I <size; I ++ ) 
    { 
        COUT << Files [I] .c_str () << endl; 
    } 
}

operation result:

 

the above.

Still my favorite people

Guess you like

Origin www.cnblogs.com/Toya/p/11207293.html