Safe cross-platform function to get normalized path

前端 未结 2 1743
逝去的感伤
逝去的感伤 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:42

    I think the closest you're going to get to platform independence are the POSIX libraries. In particular you'll wanna check out unistd.h which unfortunately I don't believe has a 'normalized' path concept. If I remember correctly the standard itself doesn't even know much about directories much less relative ones.

    To get better than that I think you'll need to roll your own path goodies.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题