Results 1 to 6 of 6

Thread: current directory?

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    current directory?

    How can I obtain the current working directory in a c/c++ program (running in Linux/Unix)? Thanks.

  2. #2

    Re:current directory?

    Code:
    char *foo = getenv("PWD");

  3. #3
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re:current directory?

    char* dir = getcwd();

  4. #4
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re:current directory?

    Both methods worked well except, getcwd is used

    Code:
    const int size = 128;
    char dir[size];
    getcwd(dir, size);
    among other ways. That answered my question, so thanks. But this brings up another question.

    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&quot, the user home directory (like "/home/user/.myprog&quot, 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.

  5. #5
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re:current directory?

    Thats what the configuration file is for..
    Just to outline it:
    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.");
    Just look for the things, where they usualy are..

    Else make a ./configure script that sends it along ei:
    C-code:
    Code:
    #ifdef __LOCATE_CONF__
    #define CONF __LOCATE__CONF__
    #else
    #define CONF "/etc/program.rc"
    #endif
    Compile code:
    Code:
    gcc -o myprog.o -D__LOCATE__CONF__=\"/new/conf/place\" -c myprog.c

  6. #6
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re:current directory?

    [quote author=redhead link=board=9;threadid=4753;start=0#47729 date=1030778507]
    Thats what the configuration file is for..
    Just to outline it:
    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.");
    Just look for the things, where they usualy are..
    [/quote]

    Does this go in the program or is it a seperate program?

    Else make a ./configure script that sends it along ei:
    C-code:
    Code:
    #ifdef __LOCATE_CONF__
    #define CONF __LOCATE__CONF__
    #else
    #define CONF "/etc/program.rc"
    #endif
    Compile code:
    Code:
    gcc -o myprog.o -D__LOCATE__CONF__=\"/new/conf/place\" -c myprog.c
    I've used programs (Loki games) that don't have the source code included and they ask you where to install the game.

Similar Threads

  1. Current forum system
    By peter in forum Announcements and Suggestions
    Replies: 1
    Last Post: 09-19-2006, 11:05 AM
  2. Current Projects
    By RickDev in forum Windows - General Topics
    Replies: 0
    Last Post: 02-14-2006, 08:22 PM
  3. slackware-current
    By KenHan in forum Linux Distros
    Replies: 3
    Last Post: 02-21-2003, 03:40 AM
  4. slackware-current
    By KenHan in forum Linux Distros
    Replies: 3
    Last Post: 12-15-2002, 06:17 PM
  5. Former (current) squids
    By cga in forum General Chat
    Replies: 2
    Last Post: 12-04-2001, 11:08 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •