Finding current path

This is a discussion on Finding current path within the Windows Programming forums, part of the Platform Specific Boards category; How do I find the current path of my program? I need to access a directory called \data and ".\data\file" ...

  1. #1
    eam
    eam is offline
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53

    Finding current path

    How do I find the current path of my program? I need to access a directory called \data and ".\\data\\file" does do it.

  2. #2
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    Re: Finding current path

    Originally posted by eam
    How do I find the current path of my program? I need to access a directory called \data and ".\\data\\file" does do it.
    Are you using MFC, or straight Win32?

    If MFC:

    1] Dialog or SDI/MDI?

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    GetCurrentDirectory() will tell you what the current directory is.
    However, you shouldn't assume that the current directory is the directory where your application resides (not that you would).

    If you want the directory that contains your running application, use GetModuleFileName().

    gg

  4. #4
    eam
    eam is offline
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    How'd I mess this up? No errors but it crashes my program... I tried I couple ways but this is the one that compiles:

    Code:
    LPTSTR file;
    DWORD num = 100; 
    GetModuleFileName(NULL,file,num);

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You have to allocate memory for the path, eg:
    Code:
    TCHAR file[MAX_PATH];
    GetModuleFileName(NULL,file,MAX_PATH);
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    eam
    eam is offline
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    There's one problem, the program name is added to the end of the path. I need to use that as a starting place for directories around the program. How should I remove the name out of it? Or is there a function that doesn't include the name?

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    You could use strrchr().
    Code:
        char pathname[MAX_PATH];
    
        GetModuleFileName(NULL, pathname, MAX_PATH);
    
        // chop off everything past last occurance of '\\'
        char *p = strrchr(pathname, '\\');
        p[1] = NULL;
    gg

  8. #8
    eam
    eam is offline
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Okay, here's what I have:

    Code:
    ///works
    GetModuleFileName(NULL,file,MAX_PATH);
    
    char *p = strrchr(file, '\\');
    p[1] = NULL;
    int t;
    for (t = 0; file[t] != '\0';t += 1) {
    file2[t] = file[t]; 
    }
    //stops working below
    file2[t+=1] = 'd' ;
    file2[t+=1] = 'a' ;
    file2[t+=1] = 't' ;
    file2[t+=1] = 'a' ;
    file2[t+=1] = '\\' ;
    file2[t+=1] = 'p' ;
    file2[t+=1] = 'i' ;
    file2[t+=1] = 'c' ;
    file2[t+=1] = '.' ;
    file2[t+=1] = 'b' ;
    file2[t+=1] = 'm' ;
    file2[t+=1] = 'p' ;
    file2[t+=1] = '\0' ;
    Basicly what I want is to make a new var (file2) and add data\pic.bmp to the end. How should I fix that?

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    strcpy()
    strcat()
    They show examples for both of those.

    gg

  10. #10
    eam
    eam is offline
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    It's working perfectly now. Thanks to everyone who help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. need some help in finding the shortest distance
    By jackhasf in forum C Programming
    Replies: 8
    Last Post: 02-15-2009, 10:58 AM
  3. Need help finding a simple 'shortest path' algorithm
    By ashley in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 12:38 PM
  4. Replies: 5
    Last Post: 01-18-2006, 10:02 PM
  5. Path problems
    By pdstatha in forum C Programming
    Replies: 0
    Last Post: 03-28-2002, 06:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21