How to get absolute path of file or directory, that does *not* exist?

前端 未结 2 994
灰色年华
灰色年华 2020-12-18 21:26

How can I determine the absolute path of a file or directory from a given relative path in C/C++ on GNU/Linux?
I know about realpath(), but it does not work

2条回答
  •  鱼传尺愫
    2020-12-18 22:15

    Try realpath. If it fails, start removing path components from the end one at a time and retrying realpath until it succeeds. Then append the components you removed back onto the result of the successful realpath call.

    If you're sure the containing directory exists and you just want to make a file there, you only have to remove at most one component.

    Another approach would be to just create the file first, then call realpath.

提交回复
热议问题