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