Reputation: 443
I've been trying to access a given file that is always in the same location with the following C++ code:
if (pcl::io::loadPCDFile<pcl::PointXYZ> ("~/Downloads/table_scene_lms400.pcd", *cloud) == -1) //* load the file
Since apparently the "~" is not working under C++, various posts on the internet suggest using /home/Downloads/... instead. I do not however seem to get it to work. I keep getting an error saying that the given file under above path cannot be found.
What is the correct way to access an absolute file path in C++?
Thanks very much, and sorry for the basicness of the question!
Upvotes: 2
Views: 11534
Reputation: 48605
C++
runtime doesn't expand file paths. What I do is something like this:
#include <cstdlib>
std::string const HOME = std::getenv("HOME") ? std::getenv("HOME") : ".";
std::ifstream myfile(HOME + "/path/in/home/folder.txt");
Upvotes: 3
Reputation: 9991
Shells such as bash and zsh expand ~
to the home directory. The function you used doesn't. You will have to expand it some other way. You can cd ~
and then pwd
to figure out where the shell thinks your home directory is and use that. Alternatively you can launch a shell and ask it for the home directory or try an environment variable or use some library such as Qt: QDir::homePath()
.
Upvotes: 5
Reputation: 409166
The tilde ~
is expanded by the shell, it's not a universal part of paths.
I suggest you use the environment variable HOME
to get the path to the current users home-directory, and then append the rest of the path.
Upvotes: 2