budekatude budekatude -3 years ago 143
C++ Question

Absolute file path in c++ under Linux

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 and 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!

Answer Source

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().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download