PDA

View Full Version : [SOLVED] How to get the absolute location of a file? (C)


Vadi
October 13th, 2007, 08:10 PM
Hi all,

I'm doing some programming in C, and while I can read, and write to files with the stdio.h library just fine, it doesn't seem to provide a function where I can get the absolute location of a file - like starting from the file system, then /home, then /user name and such.

Does anyone know of a way I can get about this limitation?

Thank you.

Lux Perpetua
October 13th, 2007, 08:26 PM
I don't understand the question. Get the absolute location...based on what? If you don't know the path and name of the file, what do you know about it?

Vadi
October 13th, 2007, 08:31 PM
The program can interact fine with the files in it's own folder, for that, I just need to specify the file's name only.

But how can I get the absolute location based on the start of the file system. Like "home/myaccount/myprogram/file.txt", as an example.

fdrake
October 13th, 2007, 08:35 PM
you mean you cannot use files that are outside the folder you're working. i am not a c programmer but i am interested in it.:-o

vambo
October 13th, 2007, 08:37 PM
Have a look at getenv

path = getenv ("$HOME")should return /home/user_name
in "path" - check the syntax - I did this off the top of my head - which is not as reliable as I'd like :(

Ptero-4
October 13th, 2007, 08:40 PM
What the OP meant is that he is making his program read and write to a plain file whose name is hardcoded in the program, but he currently needs to have the file in the same path as the program binary, and he wants to know how to tell the program to look in a specific path (in the format /path/to/the/file.txt) no matter if the file and program binary are on different parts of the filesystem.

Vadi
October 13th, 2007, 08:43 PM
Oh dear, I think I confused myself.

Okay, in short, I do this - open a file, that's inside the same folder as the program, so I don't need to worry about hopping dirs. I write to it, and when I want to output "Data saved to path_to_file/file.txt", hence the absolute location. Like for windows users, it should start with C:/ and such.

Does anyone know how can I do that? I suppose all I need is the absolute location of the program itself, really.

CptPicard
October 13th, 2007, 08:44 PM
When you start your process, it's got a working directory which is implicitly added in front of the path you use in your filesystem calls. You can figure it out by using the getcwd (http://www.gnu.org/software/libc/manual/html_node/Working-Directory.html#Working-Directory) function :)

Vadi
October 13th, 2007, 08:45 PM
Awesome, thank you. I'll take a look.

Lux Perpetua
October 13th, 2007, 08:45 PM
So you want to basically want to prepend the current working directory to the filename? getenv("PWD") will get you the current directory, and you can work from there.

CptPicard
October 13th, 2007, 08:47 PM
Why mess around with env variables when there is a clear-cut standard library call? (that I found out by a simple googling of "glibc currect working directory" or somesuch) :)

Vadi
October 13th, 2007, 09:03 PM
Thank you! The getcwd function did it.