Safe cross-platform function to get normalized path

前端 未结 2 1713
逝去的感伤
逝去的感伤 2021-01-19 00:09

I\'d like to have a standard function that will convert relative paths into absolute ones, and if possible I\'d like to make it as cross-platform as possible (so I\'d like t

2条回答
  •  攒了一身酷
    2021-01-19 00:46

    There's not a single, universal function you can call, since there's no such function in the C or C++ standard libraries. On Windows, you can use GetFullPathName. On Linux, Mac OS X, and other *Unix-based systems, you can use the realpath(3) function, which as a bonus also resolves symbolic links along the way.

    Beware: Any solution to this is only reliable in a single-threaded program. If you're using multiple threads, another can go out and change the working directory out from under you unexpectedly, changing the path name resolution.

提交回复
热议问题