C++ 如何仅打印完整路径名的文件部分?

C++ 如何仅打印完整路径名的文件部分?,c++,c,printf,substring,C++,C,Printf,Substring,下面是我面临的问题的快速重现: #include <iostream> #include <cstring> int main() { char path[100] = "home/user/cvs/test/VSCode/Test.dll"; char *pos = strrchr(path, '/'); if (pos != NULL) { *pos = '\0'; } printf("%s"

下面是我面临的问题的快速重现:

#include <iostream>
#include <cstring>

int main()
{
    char path[100] = "home/user/cvs/test/VSCode/Test.dll";
    char *pos = strrchr(path, '/');
if (pos != NULL) 
{
   *pos = '\0'; 
}
    printf("%s", path);
}
但是,对于我当前的代码,输出是:

home/user/cvs/test/VSCode

基本上,我的代码打印最后一个“/”之前的所有内容,但我需要打印最后一个“/”之后的所有内容。

调用
strrchr
后,
pos
将指向最后一次出现的
/
。如果将其前进一步,它将指向文件名的开头:

char*pos=strrchr(路径“/”);
如果(位置!=NULL)
{
++pos;
printf(“%s”,pos);/*注意-打印pos,而不是路径*/
}

您可以执行
printf(“%s”,位置+1)(并省略
*pos='\0'
)。另外,我会做
charpath[]=“home/user/cvs/test/VSCode/test.dll”
(省略数组大小,因为编译器将推断它)。
\0
表示字符串的结尾,这就是为什么在更新它时,您会看到最后一个/谢谢!!如果
pos
NULL
(意味着没有
/
字符),您可能应该将其设置为
路径
,并将
printf
移动到测试之外。
home/user/cvs/test/VSCode