linux c 目录最后是则去掉例如opttest去掉后为opttest 给出实现代码
下面是一个示例代码,用于去除路径末尾的斜杠:
#include <stdio.h>
#include <string.h>
void removeTrailingSlash(char* path) {
int len = strlen(path);
if (len > 1 && path[len-1] == '/') {
path[len-1] = '\0';
}
}
int main() {
char path[] = "/opt/test/";
removeTrailingSlash(path);
printf("%s\n", path);
return 0;
}
这个例子中,removeTrailingSlash 函数接受一个字符串 path 作为参数,并检查字符串最后一个字符是否为斜杠。如果是斜杠,则将最后一个字符替换为字符串结束符号 \0,从而去除末尾的斜杠。
在 main 函数中,我们声明了一个路径字符串 path,并将其作为参数传递给 removeTrailingSlash 函数。然后,我们打印修改后的路径字符串。输出结果为 /opt/test
原文地址: https://www.cveoy.top/t/topic/itY1 著作权归作者所有。请勿转载和采集!