代码之家  ›  专栏  ›  技术社区  ›  Rob Lao

在GNU/Linux上查找Windows的PathAppend的替代品

  •  0
  • Rob Lao  · 技术社区  · 15 年前

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

    1 回复  |  直到 15 年前
        1
  •  1
  •   Michael Burr    15 年前

    下面是一个quick-n-dirty,next-to-untested版本,它使用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() 返回失败?