功能到文件路径分割成路径和文件 [英] function to split a filepath into path and file

查看:101
本文介绍了功能到文件路径分割成路径和文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一个函数:

 无效split_path_file(字符** P,焦炭**楼的char * PF)
{
    // malloc和集* p来的文件路径,malloc和设置* F到文件名
    // PF为指针,以完整的文件和路径C:\\海绵\\鲍勃\\ square.pants
    //编辑:在其原始状态,离开粉煤
}

最新最好的方式做到这一点?


解决方案

 无效split_path_file(字符** P,焦炭**楼的char * PF){
    字符*斜线= PF,*旁;
    而((NEXT = strpbrk(斜线+ 1,\\\\ /)))=斜线旁边;
    如果(PF =斜线!)斜线++;
    * P = strndup(PF,斜线 - PF);
    * F =的strdup(斜线);
}

(如 PF ==削减,那么就没有目录部分。)

Lets say I have a function:

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
}

Whats the best way to accomplish this?

解决方案

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);
}

(If pf == slash, then there is no directory component.)

这篇关于功能到文件路径分割成路径和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆