gpt4 book ai didi

c - 从路径获取文件夹

转载 作者:行者123 更新时间:2023-11-30 15:31:39 26 4
gpt4 key购买 nike

假设我有一个字符串路径(就像这个):

/ROOT/DIRNAME/FILE.TXT

如何获取 file.txt 的父文件夹(在本例中为 DIRNAME)?

最佳答案

对于其中至少应包含一个目录的路径:

char str[1024];   // arbitrary length. just for this example
char *p;
strcpy(str, "/ROOT/DIRNAME/FILE.TXT"); // just get the string from somewhere
p = strrchr(str, '/');
if (p && p != str+1)
{
*p = 0;
p = strrchr(p-1, '/');
if (p)
print("folder : %s\n", p+1); // print folder immediately before the last path element (DIRNAME as requested)
else
printf("folder : %s\n", str); // print from beginning
}
else
printf("not a path with at least one directory in it\n");

关于c - 从路径获取文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24616979/

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