【问题标题】:Find a replacement for Windows' PathAppend on GNU/Linux在 GNU/Linux 上寻找 Windows PathAppend 的替代品
【发布时间】:2011-03-14 04:33:53
【问题描述】:

C 编程,有没有什么好的方法来管理路径字符串,而不是在 Linux 上使用像 strcat 这样的 C 字符串 API?相当于 Windows 的 PathAppend 会很棒。谢谢!

【问题讨论】:

  • 我没有窗户。那么你能解释一下PathAppend 执行了什么魔法,而简单的strcat 看不到吗?

标签: c linux path


【解决方案1】:

这是一个快速且未经测试的版本,它在两者之间使用 Unix 友好的“/”分隔符连接路径:

int PathAppend( char* path, char const* more)
{
    size_t pathlen = strlen( path);

    while (*more == '/') {
        /* skip path separators at start of `more` */
        ++more;
    }

    /* 
     * if there's anything to add to the path, make sure there's 
     * a path separator at the end of it
     */

    if (*more && (pathlen > 0) && (path[pathlen - 1] != '/')) {
        strcat( path, "/");
    }

    strcat( path, more);

    return 1; /* not sure when this function would 'fail' */
}

请注意,在我看来,这个函数应该有一个指示目标大小的参数。我也没有实现 Win32 删除“。”的记录功能。和路径开头的“..”组件(为什么会在那里?)。

还有,什么会导致Win32的PathAppend()返回失败?

使用(和/或修改)风险自负...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多