absolute path

How to find out the absolute path of a file in C/C++?
Thanks

You mean that you're given a filename like "foo.txt" and you want to find it in the filesystem to see that it's in a given path, example: /somepath/to/myfiles/foo.txt?

stat the file, get the inode number of the file, call popen with
ls -i /somepath/to/myfiles/foo.txt
and check the inode you get back. It has to match the inode number you started with.

The other case is nasty - when you have no idea where the file is located.
The reason is that the one unique identifier for a file in a given filesystem is the inode.
It can be duplicated in all of the other filesystems mounted on the machine.
So you could have several foo.txt files with the same inode number.

You can try using ftw() or nftw(), or call find from a popen() call.
It is not efficient to use either of these from the root directory /, plus it is possible to find more than one matching filename/inode.

The find syntax is:

find / -type f  -name foo.txt -inode <inode number>

Edit: note that st_dev plus st_inode give a unique identifier for a file.
You have to call ftw(), as there is no way to identify an st_dev value for find to use.

it is a bit more complicated than that.
to be more specific, when given such a root folder that has foldera and folderb under it.
root---foldera
|-folderb
there is a file called afile in foldera.
right now, i am in folderb and I am trying to open afile by calling a function. the parameter of this function can be either "../foldera/afile" or "/root/foldera/afile". Now, in this function, I want to find out the absolute path of "afile" if the parameter is not absolute yet.
any help is appreciated
thanks

You do realize there can be more than one hard link for a given file, plus there may be multiple symlinks as well. When you call fopen ("/path/to/foo", "r"), foo can easily be a link. So you may not have the real path to start with.

So going "up" the file tree is not going to give you a perfect answer all the time.

I'm not giving you a hard time, it's just that working out whre a file is in all the filesystems on one box is a daunting task. find / -name <> -inode <> is about as good as it gets, even if it is really slow.

In your example, if you're postive foldera or folderb or folderc are under /root then your task is:

get st_dev and st_ino for the file you do have
with popen(), "find /root -name afile -inode st_ino", then stat all of the files returned.
check the st_dev values, when you have a match you have the file.

If you are in, ahem, /directoryb (sorry, but I hate calling directories "folders"), you can call getcwd() to obtain "/directoryb". Since "../directorya/filea" does not start with a /, it must be a relative path. So prepend your current directory to it to get "/directoryb/../directorya/filea" which is an absolute path to the file in question. You could parse this to remove "/.." and the word preceding "/.." if you really need to simplify the path. But no matter what you do, it is possible for files to have multiple absolute paths that do not involve ".." due to symbolic links and loopback mounts.