LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 10-28-2010, 05:59 PM   #1
Basel
Member
 
Registered: Feb 2004
Location: United States
Distribution: Ubuntu 10.10
Posts: 319

Rep: Reputation: 30
How to get the full path of a file in C?


Is there a way to get the full path of a file in C? I have a method that accepts a file name as an argument and is supposed to read the content of the file. The file name might include "../" so this might lead to accessing files outside of the current directory. I tried to use fstat but I could not figure out how to get the full path.

Code:
void readFile(char *filename) {
	FILE *fp;
	//char filename[] = "../../debug.log";
	fp = fopen(filename, "r");
	if (fp == NULL){
		printf("Cannot open file[%s]\n", filename);
		return;
	}
	// read the file
	fclose(fp);
}
UPDATE: follow-up posted on post #4

Last edited by Basel; 10-30-2010 at 10:55 PM.
 
Old 10-28-2010, 06:14 PM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,221

Rep: Reputation: 307Reputation: 307Reputation: 307Reputation: 307
"realpath()" can do it.
 
Old 10-28-2010, 06:42 PM   #3
Basel
Member
 
Registered: Feb 2004
Location: United States
Distribution: Ubuntu 10.10
Posts: 319

Original Poster
Rep: Reputation: 30
Thanks
Here is an example
Code:
char filename[] = "../../../../usr/include/stdio.h";
char *path = realpath(filename, NULL);
if(path == NULL){
	printf("cannot find file with name[%s]\n", filename);
} else{
	printf("path[%s]\n", path);
	free(path);
}
// output
path[/usr/include/stdio.h]
cannot find file with name[../../../usr/include/stdio.h]
 
Old 10-30-2010, 10:52 PM   #4
Basel
Member
 
Registered: Feb 2004
Location: United States
Distribution: Ubuntu 10.10
Posts: 319

Original Poster
Rep: Reputation: 30
Is there a method that would resolve a file name into a full path?
realpath works for existing files but I need to resolve a file name to a full path even if the file does not exist.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Get full path of a file get_to_know Programming 1 08-03-2009 12:34 AM
how can i find full path of file? DoME69 Programming 5 03-04-2009 02:11 PM
full path of a file ttilt Linux - General 1 11-08-2005 06:02 PM
get full path of uploaded file in php spoody_goon Programming 2 05-10-2005 07:32 PM
How to parse full path of file name in bash ? hq4ever Programming 2 03-28-2005 03:31 PM


All times are GMT -5. The time now is 01:06 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration