Finding the path of a file

This is a discussion on Finding the path of a file within the C Programming forums, part of the General Programming Boards category; Can someone tell me how to find the path of a file that is in the same directory where the ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Finding the path of a file

    Can someone tell me how to find the path of a file that is in the same directory where the executable file is? I tried using the GetFullPathName() function but I think I did something wrong. Thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    getcwd() will tell you the current working directory.

    Post your attempt at getting GetFullPathName() to work.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Code:
    #include <windows.h>
    
    char path[100];
    
    GetFullPathName("cliente.html", sizeof(path), path, NULL);

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Trying here got me this which gave me this:
    Code:
    DWORD GetFullPathName(
      LPCTSTR lpFileName,
      DWORD nBufferLength,
      LPTSTR lpBuffer,
      LPTSTR* lpFilePart
    );
    Are you sure you want the last parameter to be NULL?
    Last edited by dwks; 08-07-2005 at 12:35 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Code:
    #include <windows.h>
    
    char path[100] = "";
    char *filename[100] = {NULL};
    
    FindExecutable("cliente.html", NULL, linha);
    GetFullPathName("cliente.html", sizeof(path), path, filename);
    ShellExecute(NULL, "open", linha, filename, NULL, SW_MAXIMIZE);
    I'm trying to open a .html file that is located in the same directory where the executable file is with the default browser but this is not working. What am I doing wrong now? Thanks.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can get the path to the current executable with GetModuleFileName. There is no need to call FindExecutable.
    Code:
    #include <windows.h>
    #include <shlwapi.h>
    #if defined(_MSC_VER)
    #  pragma comment(lib, "shlwapi.lib")
    #endif
    
    TCHAR szExePath[MAX_PATH];
    TCHAR szHtmlPath[MAX_PATH];
    
    GetModuleFileName(NULL, szExePath, MAX_PATH);             // eg. C:\test\myprogram.exe
    PathRemoveFileSpec(szExePath);                            // eg. C:\test
    PathCombine(szHtmlPath, szExePath, TEXT("cliente.html")); // eg. C:\test\cliente.html
    ShellExecute(NULL, TEXT("open"), szHtmlPath, NULL, NULL, SW_MAXIMIZE);

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Thank you

  8. #8
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Can you please explain the marked line, what does it mean by "module"?

    Thank you.


    Quote Originally Posted by anonytmouse
    You can get the path to the current executable with GetModuleFileName. There is no need to call FindExecutable.
    Code:
    #include <windows.h>
    #include <shlwapi.h>
    #if defined(_MSC_VER)
    #  pragma comment(lib, "shlwapi.lib")
    #endif
    
    TCHAR szExePath[MAX_PATH];
    TCHAR szHtmlPath[MAX_PATH];
    
    GetModuleFileName(NULL, szExePath, MAX_PATH);             // eg. C:\test\myprogram.exe
    PathRemoveFileSpec(szExePath);                            // eg. C:\test
    PathCombine(szHtmlPath, szExePath, TEXT("cliente.html")); // eg. C:\test\cliente.html
    ShellExecute(NULL, TEXT("open"), szHtmlPath, NULL, NULL, SW_MAXIMIZE);
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Module, as in executable file or .dll or something like that, I think.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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