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?
+9
A:
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;
AraK
2009-11-02 16:05:28
+9
A:
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.
pmg
2009-11-02 16:51:38
Thanks for the edit, Jonathan; and especially for the summary.
pmg
2009-11-02 20:28:08
+1
A:
In shell scripts, the command "readlink -f" has the functionality of realpath().
Ivan
2010-03-02 12:26:36