LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2007, 06:23 AM   #1
zacodi
LQ Newbie
 
Registered: Jul 2007
Posts: 2

Rep: Reputation: 0
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);

}
 
Old 07-05-2007, 07:03 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.
 
Old 07-06-2007, 12:14 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 07-06-2007, 01:23 PM   #4
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
The question makes no sense. Paths are, by definition, relative unless they are absolute (in which case, they can't be relative...).
 
Old 07-06-2007, 01:55 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help in displaying the relative path zacodi Linux - Newbie 2 07-05-2007 04:14 PM
Question in displaying the relative path oliviapesayco Programming 1 06-30-2007 03:27 PM
Relative Path? mikeshn Linux - General 3 05-10-2002 04:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration