C中字符向右查找实现截取内容

tech2023-02-03  116

#include <stdio.h> #include <sys/types.h> #include <string.h> int trim_dots(const char * path) { char * pos = NULL; pos = strrchr(path, '/'); printf("%s\n", pos); if(pos == NULL) { perror("strrchr"); return -1; } if((strcmp(pos+1, ".") == 0) || (strcmp(pos+1, "..") == 0)) return 0; else return -2; return 0; } int main(int argc, const char *argv[]) { int ret = -1; const char * path1 = "12340/."; const char * path2 = "abcd/.."; ret = trim_dots(path1); if(ret != 0) { fprintf(stderr, "trim_dots error %d\n", ret); } else { fprintf(stdout, "trim_dot ok!\n"); } ret = trim_dots(path2); if(ret != 0) { fprintf(stderr, "trim_dots error %d\n", ret); } else { fprintf(stdout, "trim_dots ok!\n"); } return 0; }

测试结果

最新回复(0)