Pfadumgebung beim Programmstart feststellen



  • Gibt es eine Möglichkeit die aktuelle Pfadumgebung zu ermitteln???

    und wie sieht das im code aus??

    danke



  • char *pfad = getenv("PWD");
    


  • Oder auch

    #include <unistd.h>
    /*...*/
    char pfad[256];
    getcwd(pfad, 256);
    


  • variante 1 stürzt ab wenn ich mir die variable pfad ausgeben lassen will..

    variante 2 geht nicht da mein visual c++ die <unistd.h> nicht kennt



  • Du merkst an dieser Stelle schon das ist total besc^Wplattformabhängig. Die beiden Beispiele funktionieren unter Unix.



  • nun gut.. hat jemand ne idee wie ich das auch auf visual c++ 6.0 erreiche??



  • Im Zweifel GetCurrentDirectory oder sowas.



  • super! danke



  • 0xdeadbeef schrieb:

    char *pfad = getenv("PWD");
    

    Ersetze hier hier "pwd" durch "path"

    oder nutze die MS main erweiterung, dann kannst du auf das gesamte Environment zum Start des Programmes zugreifen.


    {
    program-statements
    }

    Microsoft Specific —>

    envp

    A pointer to an array of environment strings. It can be declared as an array of pointers to char (char *envp[ ]) or as a pointer to pointers to char (char **envp). If your program uses wmain instead of main, use the wchar_t data type instead of char. The end of the array is indicated by a NULL pointer. The environment block passed to main and wmain is a “frozen” copy of the current environment. If you subsequently change the environment via a call to putenv or _wputenv, the current environment (as returned by getenv/_wgetenv and the _environ/ _wenviron variable) will change, but the block pointed to by envp will not change. This argument is ANSI compatible in C, but not in C++.

    END Microsoft Specific

    Oder auch

    #include <unistd.h>
    /.../
    char pfad[256];
    getcwd(pfad, 256);

    Nimm anstelle von <unistd.h> die <direct.h> und anstelle von getcwd _getcwd



  • 0xdeadbeef schrieb:

    Im Zweifel GetCurrentDirectory oder sowas.

    Das Current Directory muß nicht zwangsweise das des Programmes sein!

    Besser ist:

    char szPfad[MAX_PATH], szBuffer[MAX_PATH];
    char *pFilename;
    GetModuleFileName (NULL, szBuffer, MAX_PATH);
    GetFullPathName (szBuffer, sizeof (szBuffer), szPfad, &pFilename);
    szPfad[pFilename-szPfad] = '\0';
    

    Jetzt enthält szPfad den Pfad Deines Programmes.


Anmelden zum Antworten