我想保存字符串中的最后一个字符?任何想法如何做到这一点?如何保存C中最后一个字符串?
目标:我正在编写一个程序,其中一项任务是使用Unix/POSIX搜索文件扩展名。举例来说,如果用户输入:
./r_client本地主机-name“的* .c”
,应尽可能返回名称.c文件类型的扩展名的所有文件。因此,我想抓住最后一个字符的strcat它下面的代码
,然后通过搜索我的目录如下:
下面是使用正则表达式
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
int main(int argc, char *argv[]){
regex_t regex;
int reti;
char msgbuf[100];
char *name = "*.c";
// grab last char, strcat it to -> "[a-zA-Z0-9]\.c$"
// because it could be '.h', '.x', etc.
/* Compile regular expression */
reti = regcomp(®ex, "[a-zA-Z0-9]\.c$", 0);
if(reti){ fprintf(stderr, "Could not compile regex\n"); exit(1); }
/* Execute regular expression */
reti = regexec(®ex, "e09s*fdf.ec", 0, NULL, 0);
if(!reti){
puts("Match");
}
else if(reti == REG_NOMATCH){
puts("No match");
}
else{
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(1);
}
/* Free compiled regular expression if you want to use the regex_t again */
regfree(®ex);
return 0;
}
//然后的示例代码调用( “e09s * fdf.ec” - 这是一个测试) - 它应该怎么失败的W/.C
reti = regexec(®ex, "e09s*fdf.ec", 0, NULL, 0);
将被替换为:dir_entry-> d_name
除非有更好的方法来做到这一点?
感谢您的帮助提前
你想支持壳水珠的文件名?如果这是你想要做的,使用'glob()'或'wordexp()'。没有必要重新发明轮子。 – fuz 2015-04-06 22:49:58