Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute file path in c++ under Linux

Tags:

c++

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

like image 802
budekatude Avatar asked Jul 29 '26 13:07

budekatude


1 Answers

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

like image 56
nwp Avatar answered Jul 31 '26 04:07

nwp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!