Getting absolute path of a file

前端 未结 4 1079
说谎
说谎 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:27

    Use realpath().

    The realpath() function shall derive, from the pathname pointed to by file_name, an absolute pathname that names the same file, whose resolution does not involve '.', '..', or symbolic links. The generated pathname shall be stored as a null-terminated string, up to a maximum of {PATH_MAX} bytes, in the buffer pointed to by resolved_name.

    If resolved_name is a null pointer, the behavior of realpath() is implementation-defined.


    The following example generates an absolute pathname for the file identified by the symlinkpath argument. The generated pathname is stored in the actualpath array.

    #include 
    ...
    char *symlinkpath = "/tmp/symlink/file";
    char actualpath [PATH_MAX+1];
    char *ptr;
    
    
    ptr = realpath(symlinkpath, actualpath);
    

提交回复
热议问题