LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help in displaying the relative path in C (https://www.linuxquestions.org/questions/programming-9/need-help-in-displaying-the-relative-path-in-c-566798/)

zacodi 07-05-2007 06:23 AM

Need help in displaying the relative path in C
 
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);
}

fstat (fileDesc, & buf);

printf ("filename : %s \n", argv[1]);
printf ("relative path : \n");
printf ("Protection Mode : %o ", buf.st_mode);
printf ("Size : %ld\n", buf.st_size);

close (fileDesc);
return (0);

}

wjevans_7d1@yahoo.co 07-05-2007 07:03 AM

Three comments.
  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.
  2. To answer your question, first you need to tell us exactly what you mean by "relative path".
  3. 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.

Hope this helps.

theNbomr 07-06-2007 12:14 PM

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.

dawkcid 07-06-2007 01:23 PM

The question makes no sense. Paths are, by definition, relative unless they are absolute (in which case, they can't be relative...).

theNbomr 07-06-2007 01:55 PM

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.

--- rod.


All times are GMT -5. The time now is 04:36 PM.