c - 如何在C中保存字符串的最后一个字符?

标签 c unix posix rpc

我正在尝试保存字符串中的最后一个字符?关于如何做到这一点有什么想法吗?

目标:我目前正在编写一个程序,其中任务之一是使用 Unix/POSIX 搜索文件扩展名。例如,如果用户输入:

./r_client localhost -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(&regex, "[a-zA-Z0-9]\.c$", 0);
        if( reti ){ fprintf(stderr, "Could not compile regex\n"); exit(1); }

/* Execute regular expression */
        reti = regexec(&regex, "e09s*fdf.ec", 0, NULL, 0);
        if( !reti ){
                puts("Match");
        }
        else if( reti == REG_NOMATCH ){
                puts("No match");
        }
        else{
                regerror(reti, &regex, 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(&regex);

        return 0;
}

//然后调用 ("e09s*fdf.ec"- 这是一个测试) - .c 会如何失败

reti = regexec(&regex, "e09s*fdf.ec", 0, NULL, 0);

将替换为:dir_entry->d_name

除非有更好的方法来做到这一点?

感谢您提前提供的帮助

最佳答案

我想你想要这样的东西:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char dest[255];

char get_last_char(char* s)
{
        return s[strlen(s) - 1];
}

int main(int argc, char *argv[])
{
        char c = get_last_char(argv[1]);
        char s[2];
        s[0] = c;
        s[1] = '\0';

        strcat(dest, "[a-zA-Z0-9]\\.");
        strcat(dest, s);
        strcat(dest, "$");
        printf("%s\n", dest);

        exit(0);
}

示例:

$ ./concatenate1 "*.x"
[a-zA-Z0-9]\.x$
$ ./concatenate1 "*.c"
[a-zA-Z0-9]\.c$
$ ./concatenate1 "*.h"
[a-zA-Z0-9]\.h$

此外,如果您发现从长远来看更容易,请考虑使用 snprintf() 而不是多个 strcat()

编辑: 下面的代码应该可以完成这项工作。缺少一件事并标记为 TODO - 验证。您永远无法确定用户将传递正确的数据。如果您的代码依赖于点后不超过 x 个字符,请检查 remaining_chars_count 并执行您认为正确的操作。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
        char *temp = strstr(argv[1], ".");
        if (!temp)
                {
                        printf("No dot found.\n");
                        exit(1);
                }

        unsigned int remaining_chars_count = strlen(argv[1]) - (temp - argv[1]) - 1;

        /* TODO: add validation at this point! */

        size_t output_string_length = remaining_chars_count;

        char output_string[output_string_length];
        unsigned output_string_count = remaining_chars_count;

        size_t i = strlen(argv[1]);
        for (i; i > strlen(argv[1]) - remaining_chars_count; i--)
                {
                        output_string[output_string_count - 1] = argv[1][i - 1];
                        --output_string_count;
                }

        output_string[output_string_length] = '\0';
        printf("OUTPUT: %s\n", output_string);
        exit(0);

}

示例:

$ ./dot1 "*1134..a"
OUTPUT: .a
$ ./dot1 abc.a
OUTPUT: a
$ ./dot1 "*a.hx"
OUTPUT: hx

关于c - 如何在C中保存字符串的最后一个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29464486/

相关文章:

regex - 如何在 Grep 中使用反向引用

c - UNIX 域套接字 (PF_UNIX) 上的 send() 失败并出现 ENOBUFS 错误

c - for 循环中的 if 条件

c - STATIC 局部变量的初始化是否可以被 'case' 标签跳过?

linux - 如何在 Shell 脚本中传递超过 10 个参数

unix - Grep 列出文件一次

c - 错误 : case label does not reduce to an integer constant for characters selected from a string literal

c - 为什么标准输入流中没有出现多个 `\n` 字符?

c - 获取系统 api 调用执行的命令的 pid

linux - 就地编辑文件