gpt4 book ai didi

c - 将文件路径拆分为路径和文件的函数

转载 作者:行者123 更新时间:2023-12-02 06:12:04 25 4
gpt4 key购买 nike

假设我有一个函数:

void split_path_file(char** p, char** f, char *pf)
{
//malloc and set *p to file path, malloc and set *f to file name
//pf is the pointer to the full file and path "C:\sponge\bob\square.pants"
// edit: leave pf in its origional state
}

实现此目标的最佳方法是什么?

最佳答案

void split_path_file(char** p, char** f, char *pf) {
char *slash = pf, *next;
while ((next = strpbrk(slash + 1, "\\/"))) slash = next;
if (pf != slash) slash++;
*p = strndup(pf, slash - pf);
*f = strdup(slash);
}

(如果pf == slash,则没有目录组件。)

关于c - 将文件路径拆分为路径和文件的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1575278/

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