Code:char *foo = getenv("PWD");
How can I obtain the current working directory in a c/c++ program (running in Linux/Unix)? Thanks.
Code:char *foo = getenv("PWD");
char* dir = getcwd();
Both methods worked well except, getcwd is used
among other ways. That answered my question, so thanks. But this brings up another question.Code:const int size = 128; char dir[size]; getcwd(dir, size);
Linux programs (especially the large application programs like a game or text editor) seem to be spread out into about three places: the installation place (like "/usr/share/myprog", the user home directory (like "/home/user/.myprog"
, and a bin directory (which is usually a link to the executable in the "/usr/share/myprog/bin" directory, but not always).
My question is how does one program for this? The program would have to know where it is installed and what the user's home directory is in order to access the data stored there. Do I have this right? Thanks.
Thats what the configuration file is for..
Just to outline it:
Just look for the things, where they usualy are..Code:char conf_file[256]; FILE* my_file = NULL; sprintf(conf_file, "%s/.%src", getenv("HOME"), PROGRAM_NAME); if(access(conf_file, F_OK)) { if(access(conf_file, R_OK)) my_file = fopen(conf_file, "r"); else error("Conf [%s] file can not be opened for reading.", conf_file); } else { sprintf(conf_file, "/etc/%s.rc", PROGRAM_NAME); if(access(conf_file, F_OK) { if(access(conf_file, R_OK)) my_file = fopen(conf_file, "r"); else error("Conf file [%s] can not be opened for reading.", conf_file); } } if(!my_file) error(No conf file found.");
Else make a ./configure script that sends it along ei:
C-code:
Compile code:Code:#ifdef __LOCATE_CONF__ #define CONF __LOCATE__CONF__ #else #define CONF "/etc/program.rc" #endif
Code:gcc -o myprog.o -D__LOCATE__CONF__=\"/new/conf/place\" -c myprog.c
[quote author=redhead link=board=9;threadid=4753;start=0#47729 date=1030778507]
Thats what the configuration file is for..
Just to outline it:
Just look for the things, where they usualy are..Code:char conf_file[256]; FILE* my_file = NULL; sprintf(conf_file, "%s/.%src", getenv("HOME"), PROGRAM_NAME); if(access(conf_file, F_OK)) { if(access(conf_file, R_OK)) my_file = fopen(conf_file, "r"); else error("Conf [%s] file can not be opened for reading.", conf_file); } else { sprintf(conf_file, "/etc/%s.rc", PROGRAM_NAME); if(access(conf_file, F_OK) { if(access(conf_file, R_OK)) my_file = fopen(conf_file, "r"); else error("Conf file [%s] can not be opened for reading.", conf_file); } } if(!my_file) error(No conf file found.");
[/quote]
Does this go in the program or is it a seperate program?
I've used programs (Loki games) that don't have the source code included and they ask you where to install the game.Else make a ./configure script that sends it along ei:
C-code:
Compile code:Code:#ifdef __LOCATE_CONF__ #define CONF __LOCATE__CONF__ #else #define CONF "/etc/program.rc" #endif
Code:gcc -o myprog.o -D__LOCATE__CONF__=\"/new/conf/place\" -c myprog.c
Bookmarks