开发者

How do I get the full path for a filename command-line argument?

I've found lots of libraries to help with parsing command-line arguments, but none of them seem to deal开发者_运维百科 with handling filenames. If I receive something like "../foo" on the command line, how do I figure out the full path to the file?


You could use boost::filesystem to get the absolute path of a file, from its relative path:

namespace fs = boost::filesystem;
fs::path p("test.txt");
fs::path full_p = fs::complete(p); // complete == absolute
std::cout << "The absolute path: " << full_p;


POSIX has realpath().

#include <stdlib.h>
char *realpath(const char *filename, char *resolvedname);

DESCRIPTION
The realpath() function derives, from the pathname pointed to by filename, an absolute pathname that names the same file, whose resolution does not involve ".", "..", or symbolic links. The generated pathname is stored, up to a maximum of {PATH_MAX} bytes, in the buffer pointed to by resolvedname.


Boost.Filesystem


In shell scripts, the command "readlink -f" has the functionality of realpath().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜