gpt4 book ai didi

c - 从 C 中的路径字符串实现中获取文件名

转载 作者:太空宇宙 更新时间:2023-11-04 01:25:19 24 4
gpt4 key购买 nike

谁能告诉我如何在不使用 C 的字符串内置函数的情况下在 C 中实现 GetFileName() 函数。例如C:\Program Files\hello.txt

输出:hello.txt

最佳答案

循环查找最后一个分隔符的字符串。如果 /\: 是有效的路径分隔符 (Windows):

char *getFileName(char *path) {
char *retVal = path, p;
for (p = path; *p; p++) {
if (*p == '/' || *p == '\\' || *p == ':') {
retVal = p;
}
}
return retVal;
}

关于c - 从 C 中的路径字符串实现中获取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32477786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com