Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Oct 18th, 2005, 9:31 AM   #1
mevuorin
Newbie
 
Join Date: Jul 2005
Posts: 10
Rep Power: 0 mevuorin is on a distinguished road
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.
mevuorin is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC