ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi. I was wondering if somebody can help me on this. I am writing a C program that finds a file and display its relative path if the file is found... I am not sure how I going to display the relative path.. Any suggestions?
int
main (int argc, char * argv[])
{
int fileDesc;
struct stat buf;
if ((fileDesc = open(argv[1], O_RDONLY)) < 0)
{
printf ("%s: %s -- File does not exist.\n",argv[0], argv[1]);
return(1);
}
When posting code, please put it between CODE markers. This is best done by clicking on the Go Advanced button at the bottom of your editing window, highlighting the part of your post that's your code, and clicking on the # button at the top of this new editing window.
To answer your question, first you need to tell us exactly what you mean by "relative path".
Once you've defined "relative path", your homework assignment should practically write itself. Which is good, because substantial help with homework assignments is usually not done here. That's because we want you to get the most out of your coursework.
You are attempting to duplicate the functionality of the find command, plus a minor wrinkle. If you are doing this for a reason other than as a learning exercise, I suggest you use find, launched as a child process with its output captured in a pipe(). From there, it should be a simple exercise in string manipulation to compare the result returned by find (plus any starting path) with the absolute path against which you are establishing relativity.
--- rod.
Paths can be equally either relative or absolute. The usual reference against which a path can be relative is the current working directory ($PWD), although technically, that needn't be so. For example, I can put in my PATH environment variable, "../../Some/Other/Directory", which would perhaps make sense when $PWD was appropriate, and may make sense at multiple different locations within a filesystem.
It is also unusual to translate an absolute to a path relative to anything, even $PWD, while the reverse translation would be more common. To me, it is a bit like saying "the answer is 7, but what is that, relative to, say, 3?". Sort of 'unsimplifying' an absolute. To apply a different metaphor, on the other hand, it can also be a bit like saying "How do I get there from here (.), rather that from some other place (/)?". It all depends on the circumstance.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.