![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 10
Rep Power: 0
![]() |
problem with relative filenames in linux
Hi
I'm working on a linux program written in c that has a config file. I'd like users to be able to install my program anywhere on the filesystem and still be able to read the config file from the program. So far I haven't found a good solution. Programs directory structure is this: program //base directory program/bin/ //binaries program/bin/foo //main program program/source/ //sources program/data/ //all data needed by the program program/configuration.cfg //configuration file I tried to open the configuration file with fopen( "../configuration.cfg", "r"), but found out that it only works if foo is run from the program/bin/ directory, because relative filenames are solved based on the current working directory which is the directory from where the program was run. Is there a way to change the current working directory to the directory where the program that is being run is? I guess that would solve my problem. Maybe I could parse the mains argv[0] string and use that with pwd to obtain the absolute path of configuration.cfg. It just seems like the hard way to do it. I also thought about placing the .cfg file to /etc, but that would need root permission, and I don't want to make my program installable only as root. I'm not sure if I should place .cfg files of my own programs to /etc anyway. I guess I could use environmental variable to hold the absolute path of program directory, but I'd rather not force users to set environmental variables after installing my program. I could probably use locate or find from the program to find where the configuration.cfg is located, but that seems quite crude and inefficient. Any ideas on how to make this work? Thanks in advance for any help. |
|
|
|
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|