gpt4 book ai didi

c - 将路径分成两段

转载 作者:行者123 更新时间:2023-11-30 20:41:37 24 4
gpt4 key购买 nike

Possible Duplicate:
function to split a filepath into path and file

我想将“a/b/c/directory/filename”这样的路径名拆分为“a/b/c/directory”和“filename”。在 C 中执行此操作的好方法是什么?

最佳答案

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ()
{
char path[] ="/aaa/bbb/ccc/file";
char *part1 = (char *)malloc (strlen(path));
strcpy (part1, path);
char *pos = strrchr (part1, '/');
*pos = '\0';
char *part2 = strdup (pos + 1);
printf ("%s \n%s", part1, part2 );

}

关于c - 将路径分成两段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12834385/

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