Getting absolute path of a file

前端 未结 4 1083
说谎
说谎 2020-11-27 05:52

How can I convert a relative path to an absolute path in C on Unix? Is there a convenient system function for this?

On Windows there is a GetFullPathName

4条回答
  •  伪装坚强ぢ
    2020-11-27 06:18

    There is also a small path library cwalk which works cross-platform. It has cwk_path_get_absolute to do that:

    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
      char buffer[FILENAME_MAX];
    
      cwk_path_get_absolute("/hello/there", "./world", buffer, sizeof(buffer));
      printf("The absolute path is: %s", buffer);
    
      return EXIT_SUCCESS;
    }
    

    Outputs:

    The absolute path is: /hello/there/world
    

提交回复
热议问题