Thread: getting path to my prog

  1. #1
    Unregistered
    Guest

    getting path to my prog

    i'm programming a console application for windows.
    it takes a file as an argument and parses this one. in the file keywords.cfg are the keywords which it should find in the text.

    so far so good. i want to put my programm and my keywords.cfg in the c:\winnt directory so that it can be called from anywhere. now the problem:

    fopen suggest the file to be in the directory i am currentli working on (in the console). so how can i find out the location where my programm is stored and it's config-file. (because the programm could be anywhere else an this directory included in the PATH variable.)


    thanks

    stormbringer (will register late :-)

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    What compiler are you using?

  3. #3
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    i am using lcc. why does that mater? the prog shall detect it's path at runtime.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What does this tell you?

    Code:
    int main ( int argc, char *argv[] ) {
        printf( "Here I am - %s\n", argv[0] );
        return 0;
    }

  5. #5
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    ou, that easy

    thanks. sometimes you don't see the tree because of the forest.

  6. #6
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    well, not that easy. using rundos it gives the correct pathname, but when i'm in the command shell, argv just contains the filename, not the path.

  7. #7
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    correction

    if you start the programm directly from the compiler progi, argv[0] contains the whole path because the compiler prog calls it with the full path. if you enter comandline in windows (cmd) you normaly just write the name of the programm. and thats what is in argv. just the name, not the path.

    any idea?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The more expansive solution is based on this - you need to add a bit of detail

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <limits.h>
    
    int main ( int argc, char *argv[] ) {
        char    *env, *path, *p;
        char    cwd[PATH_MAX];
    
        // if argv[0] doesn't begin with D:
        // then it's probably relative to either PATH or CWD
        printf( "%s\n", argv[0] );
        env = getenv("PATH");
    
        if ( env != NULL ) {
            // copy the string, because we're going to use strtok
            path = malloc( strlen(env) + 1 );
            strcpy( path, env );
    
            // append argv[0] onto each p, and see if the file exists
            for ( p = strtok(path,";");
                  p != NULL;
                  p = strtok(NULL,";") ) {
                printf( "Path=%s\n", p );
            }
    
            free( path );
        }
    
        // still not found, try current dir
        getcwd( cwd, sizeof(cwd) );
        printf( "CWD=%s\n", cwd );
    
        return 0;
    }

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: getting path to my prog

    Originally posted by Unregistered
    so far so good. i want to put my programm and my keywords.cfg in the c:\winnt directory so that it can be called from anywhere. now the problem:

    fopen suggest the file to be in the directory i am currentli working on (in the console). so how can i find out the location where my programm is stored and it's config-file. (because the programm could be anywhere else an this directory included in the PATH variable.)
    Maybe I'm reading you wrong, but the bold bits (above) answer themselves.. yes?

    You said the program and the config file are in c:\winnt. Then you asked where are the program and config file? Well, they're in the c:\winnt directory! But then you said the program could be elsewhere. Come on, make up your mind

    In your program, when you open keywords.cfg, use the full pathname.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Unregistered
    Guest
    they are in c:\winnt for testing purposes. they will be moved to an own folder (which the user specify themself) an included into the path.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shortest Path Maze Solver (Breadth Search Help)
    By Raskalnikov in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 07:41 PM
  2. clipping path
    By stanlvw in forum Windows Programming
    Replies: 0
    Last Post: 07-23-2008, 11:47 PM
  3. Can't figure out what keeps hanging up my program
    By shays in forum C Programming
    Replies: 7
    Last Post: 11-12-2007, 02:59 PM
  4. Shortest path problem
    By Digitalxero in forum C++ Programming
    Replies: 0
    Last Post: 10-25-2005, 05:32 PM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM