Getting absolute path of a file

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

    Try realpath() in stdlib.h

    char filename[] = "../../../../data/000000.jpg";
    char* path = realpath(filename, NULL);
    if(path == NULL){
        printf("cannot find file with name[%s]\n", filename);
    } else{
        printf("path[%s]\n", path);
        free(path);
    }
    

提交回复
热议问题