C/C ++ traverse folders

Source: Internet
Author: User

Source Address: http://blog.163.com/woshitony111@126/blog/static/71379539201262213418928/

Use of finddata_t

So how do I find files? We need a struct and several functions that may be unfamiliar to everyone. These functions and struct are in the header file of <Io. h>. The struct is struct _ finddata_t and the function is _ findfirst, _ findnext, and _ fineclose. I will explain how to use them ~

Let's talk about this struct first ~ Struct _ finddata_t, which is used to store various file information. To be honest, I have not found the specific definition code of this struct, but fortunately, the file contains a detailed introduction of member variables in _ Find. Basically, Let me translate the document:

Unsigned atrrib: storage location of file attributes. It stores an unsigned unit to indicate the attributes of a file. File attributes are represented by BITs, mainly including _ a_arch (archive), _ a_hidden (hidden), _ a_normal (normal), and _ a_rdonly (read-only), _ a_subdir (folder), _ a_system (system ). These are
In <Io. h> the macro defined in can be used directly, and its meaning is actually an unsigned integer (but this integer should be several power of 2, so as to ensure that only one digit is 1, and its bit is 0 ). Since it is a bit representation, when a file has multiple attributes, it often obtains the synthesis of several attributes by bit or. For example, the read-only + hide + system attribute should be: _ a_hidden | _ a_rdonly | _ a_system.

Time_t time_create: The time_t here is a variable type (long integer? Equivalent to long Int ?), The time_create variable is used to store the file creation time.

Time_t time_access: The last time the file was accessed.

Time_t time_write: The last time the file was modified.

_ Fsize_t size: file size. Here, the _ fsize_t should be equivalent to the unsigned integer, indicating the number of bytes of the file.

Char name [_ max_fname]: file name. Here _ max_fname is a constant macro, which is defined in the <stdlib. h> header file, indicating the maximum length of the file name.

In this way, we can infer that struct_finddata_t is roughly 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];

};

As mentioned above, this struct is used to store file information. How can we store the file information of a hard disk file in the memory space indicated by this struct? This depends on the combination of the _ findfirst, _ findnext, and _ fineclose functions.

First, let's introduce these three functions one by one ......

Long _ findfirst (char * filespec, struct _ finddata_t * fileinfo );

Return Value: If the query is successful, a long-type unique query handle (that is, a unique number) is returned ). This handle will be used in the _ findnext function. If it fails,-1 is returned.

Parameters:

Filespec: Specifies the file string. Wildcards are supported. For example, *. C indicates all files with the suffix C in the current folder.

Fileinfo: the pointer to the struct used to store file information. This struct must be declared before calling this function, but it does not need to be initialized, as long as the memory space is allocated. After the function is successful, the function puts the information of the found file into this struct.

Int _ findnext (long handle, struct _ finddata_t * fileinfo );

Return Value: if the result is successful, 0 is returned. Otherwise,-1 is returned.

Parameters:

Handle: The handle returned by the _ findfirst function.

Fileinfo: pointer to the object information structure. After finding the file, the function puts the file information in this struct.

Int _ findclose (long handle );

Return Value: 0 is returned for success, and-1 is returned for failure.

Parameters:

Handle: handle returned by the _ findfirst function.

As you can see, you can probably guess it? First use the _ findfirst function to find the first file. If it succeeds, call the _ findnext function with the returned handle to find other files. After the search is complete, use the _ findclose function to end the search. Well, yes, this is the correct idea. Next we will write a program to find all the executable files of EXE in the C: \ Windows folder.

# Include <stdio. h>

# Include <Io. h>

Const char * to_search = "C: \ WINDOWS \ *. EXE"; // the file to be searched. Wildcards are supported.

Int main ()

{

Long Handle; // The handle used for searching

Struct _ finddata_t fileinfo; // structure of the file information

Handle = _ findfirst (to_search, & fileinfo); // The first search

If (-1 = handle) Return-1;

Printf ("% s \ n", fileinfo. Name); // print the file name.

While (! _ Findnext (handle, & fileinfo) // cyclically searches for other matching files, until other matching files are found

{

Printf ("% s \ n", fileinfo. Name );

}

_ Findclose (handle); // do not forget to close the handle.

System ("pause ");

Return 0;

}

Of course, this file is searched in the specified path. How can I traverse the hard disk and find the file in the entire hard disk? You can search for Recursive file traversal methods on the network.

Careful friends may notice that I use a system function at the end of the program. This is not affected by the program itself. It serves the same purpose as the previously introduced getchar () function, just to pause it, so that we can see the output results at the command prompt. However, the system function itself is a very powerful function. Check out msdn ~ To put it simply, it is a platform for the interaction between C and the operating system. You can use this function in your program to pass Command commands to the operating system.

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.