tags:

views:

440

answers:

4

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?

+2  A: 

Boost.Filesystem

Ben Collins
+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
+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
Thanks for the edit, Jonathan; and especially for the summary.
pmg
+1  A: 

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

Ivan