In the following example, the fnmatch function is used for character wildcard, such as matching *,? .

Source: Internet
Author: User
In the following example, the fnmatch function is used for character wildcard, such as matching *,? .

Source code:

************************ ********************
* Filename: fnmatch. c
* Purpose: describes how to use fnmatch for character matching.
* Wrote by: zhoulifa (zhoulifa@163.com) Zhou Lifa (http://zhoulifa.bokee.com)
Linux enthusiasts Linux knowledge disseminators sohowhose developers are best at C Language
* Date Time: Shanghai Heavy snow day, said to have been absent for many years
* Note: Anyone can copy the code and use these documents, including your commercial use.
* But follow the GPL
* Thanks:
* Ubuntu 7.10 is completely normal.
* Google.com I usually use Google search to find many useful materials.
* Hope: more and more people are expected to contribute to the development of science and technology.
* Technology stands on the shoulders of giants and makes progress faster! Thank you for your contributions to the open source team!
**************************************** *****************************/
# Include <locale. h>
# Include <fnmatch. h>
# Include <stdio. h>
# Include <sys/types. h>
# Include <dirent. h>

Main (INT argc, char * argv [])
{
Char * pattern;
Dir * dir;
Struct dirent * entry;
Int ret;

/* Setlocale (lc_all, "zh_CN.UTF-8 ");*/

Dir = opendir (argv [2]);

Pattern = argv [1];

If (Dir! = NULL ){
While (Entry = readdir (DIR ))! = NULL ){
Ret = fnmatch (pattern, entry-> d_name, fnm_pathname | fnm_period );
If (ret = 0 ){
Printf ("% s/n", entry-> d_name );
} Else if (ret = fnm_nomatch ){
Continue;
} Else {
Printf ("error file = % s/n", entry-> d_name );
}
}
Closedir (DIR );
}
}

Compile and run this program:
Gcc-wall fnmatch. c
./A. Out "*. c"/src/mycode/C

This command will display the *. c file in the/src/mycode/C directory. That is, the matching file name is *. c
 

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.