需要了解 _findfirst 和 _findnext 如何与模式输入一起工作。
我曾经遵循代码来分析它们的功能,但结果让我感到困惑。
intptr_t handle;
struct _finddata_t fblock;
int count = 1;
char pattern[] = "*";
printf("Pattern is %s",pattern);
if((handle = _findfirst(pattern,&fblock))>0){
printf("\nfound : %d :: ",count++);
printf("%s",fblock.name);
while(_findnext(handle,&fblock) == 0){
printf("\nfound : %d :: ",count++);
printf("%s",fblock.name);
}
}
printf("\ncompleted");
//getch();
结果:我的文件夹中有四个文件。
1.ReadMe.txt
2.ReadMe1.txt
3.ReadMe1.txt_bck
4.ReadMe1.txtbck
现在,
Enter pattern :*.tx
Pattern is *.tx
completed
好的。没有带有 ext tx 的文件
Enter pattern :*.txt_
Pattern is *.txt_
completed
好的。也没有带有 ext txt_ 的文件
Enter pattern :*.txt
Pattern is *.txt
found : 1 :: ReadMe.txt
found : 2 :: ReadMe1.txt
found : 3 :: ReadMe1.txtbck
found : 4 :: ReadMe1.txt_bck
completed
???我只要求 ext txt。为什么会显示 txt_bck?
Enter pattern :*.txt*
Pattern is *.txt*
found : 1 :: ReadMe.txt
found : 2 :: ReadMe1.txt
found : 3 :: ReadMe1.txtbck
found : 4 :: ReadMe1.txt_bck
completed
现在这个和上一个有什么区别?
Enter pattern :ReadMe?.txt
Pattern is ReadMe?.txt
found : 1 :: ReadMe.txt
found : 2 :: ReadMe1.txt
completed
预期的 ”?” 通配符将只匹配一个字符,但它也匹配 0。
Enter pattern :ReadMe*.txt
Pattern is ReadMe*.txt
found : 1 :: ReadMe.txt
found : 2 :: ReadMe1.txt
found : 3 :: ReadMe1.txtbck
found : 4 :: ReadMe1.txt_bck
completed
_