The UNIX Forums
"Join the Network of UNIX System Users"


 
koporc
Newbie
Rank: 1



UID 228
Digest Posts 0
Credits 0
Posts 5
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:44 AM  Profile | P.M. 
absolute path



absolute path



how to find out the absolute path of a file in c/c++?


Top
mikebrad0927
Newbie
Rank: 1



UID 265
Digest Posts 0
Credits 0
Posts 55
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:44 AM  Profile | P.M. 
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:

code:
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.


last edited by jim mcnamara : 1 week ago at 03:17 pm.
Top
ferret77
Newbie
Rank: 1



UID 144
Digest Posts 0
Credits 0
Posts 22
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:44 AM  Profile | P.M. 
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


last edited by filedeliver : 1 week ago at 02:57 pm.
Top
hani
Newbie
Rank: 1



UID 170
Digest Posts 0
Credits 0
Posts 31
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:44 AM  Profile | P.M. 
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.
Top
 

 

All times are GMT, the time now is Mar 15, 2010 03:52 PM

Powered by Discuz! 5.0.0  © 2001-2006 UNIX Forums
Processed in 0.005237 second(s), 8 queries

Clear Cookies - Contact Us - UNIX Help - Archiver - WAP