Fnmatch (fuzzy query with readdir and opendir)

Source: Internet
Author: User

Fnmatch: int fnmatch (const char * pattern, const char * string, int flags );
The fnmatch () function checks whether the string argument matches the pattern argument, which is a shell wildcard pattern. It is used to determine whether the string conforms to the pattern structure.

Fnm_noescape
If this flag is set, the backslash is a common character instead of an escape character.
Fnm_pathname
If this flag is set, StringOnly match the slash in PatternIt cannot match the asterisk (*) or question mark (?) The metacharacters cannot match the brackets ([]) that contain the slash.
Fnm_period
If this flag is set, StringThe start point number in must match Pattern. A vertex is considered as the starting point. If it is StringThe first character, or if both
Fnm_pathname, Followed by the dot next to the slash.
Fnm_file_name
This is Fnm_pathname.
Fnm_leading_dir
If this flag (GNU extension) is set, the pattern must match String. This flag is mainly used internally for glibc and is only implemented under certain conditions.
Fnm_casefold
If this flag (GNU extension) is set, case-insensitive for pattern matching.
Return Value: 0, StringMatch
Pattern
; Fnm_nomatch, no match; or other non-zero values, if an error occurs.

(The program comes from the Network)

# Include <fnmatch. h> # include <stdio. h> # include <sys/types. h> # include <dirent. h> int main (INT argc, char * argv []) {char * pattern; dir * dir; struct dirent * entry; int ret; dir = opendir (argv [2]); // open the specified path pattern = argv [1]; // If (Dir! = NULL) {// obtain the files in the folder one by one while (Entry = readdir (DIR ))! = NULL) {ret = fnmatch (pattern, entry-> d_name, fnm_pathname | fnm_period); If (ret = 0) // pattern-compliant Structure {printf ("% s \ n", entry-> d_name);} else if (ret = fnm_nomatch) {continue ;} else {printf ("error file = % s \ n", entry-> d_name) ;}} closedir (DIR );}}

 

 

 

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.