Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C++ Application path/directory
New Posts  All Forums:Forum Nav:

C++ Application path/directory

post #1 of 7
Thread Starter 
I need a tiny bit of help.
I want to find the directory of the application itself. Basically, I want my program to copy a file, from the directory its being ran from, to another place.
The copy thing works perfectly, but I can't seem to figure out a way to find the program path.

Any help would be appreciated! thumb.gif
Citra ♥
(15 items)
 
Big Mac
(10 items)
 
 
CPUMotherboardGraphicsRAM
[Intel] Core i7 2600K [Asus] P8Z68-V Pro [Gainward] Geforce 9800 GTX+ [Corsair] Vengeance 16GB @ 1600 
Hard DriveHard DriveCoolingOS
[Kingston] HyperX 120GB SSD [Intel] 520 series 60GB [Corsair] H80 [Canonical] Ubuntu 12.10 
MonitorKeyboardPowerCase
[BenQ] XL2420T [Corsair] Vengeance K60 Performance [OCZ] ZX 850W [CM] Storm Enforcer 
MouseAudioOther
[Razer] Mamba 4G 2012 [Beats] urBeats [NZXT] 24/8/6 pin extensions 
CPUMotherboardGraphicsRAM
[Intel] Core i5-3210M [Apple] Proprietary [Intel] HD 4000 [Hynix] H5TC2G83CFR DDR3L 8GB 
Hard DriveOSMonitorKeyboard
[Samsung] 128GB SSD [Apple] OS X 10.8.2 ML 13" 2560x1600 Retina [Apple] Backlit Keyboard 
CaseMouse Pad
[Apple] Unibody [Apple] Trackpad 
  hide details  
Reply
Citra ♥
(15 items)
 
Big Mac
(10 items)
 
 
CPUMotherboardGraphicsRAM
[Intel] Core i7 2600K [Asus] P8Z68-V Pro [Gainward] Geforce 9800 GTX+ [Corsair] Vengeance 16GB @ 1600 
Hard DriveHard DriveCoolingOS
[Kingston] HyperX 120GB SSD [Intel] 520 series 60GB [Corsair] H80 [Canonical] Ubuntu 12.10 
MonitorKeyboardPowerCase
[BenQ] XL2420T [Corsair] Vengeance K60 Performance [OCZ] ZX 850W [CM] Storm Enforcer 
MouseAudioOther
[Razer] Mamba 4G 2012 [Beats] urBeats [NZXT] 24/8/6 pin extensions 
CPUMotherboardGraphicsRAM
[Intel] Core i5-3210M [Apple] Proprietary [Intel] HD 4000 [Hynix] H5TC2G83CFR DDR3L 8GB 
Hard DriveOSMonitorKeyboard
[Samsung] 128GB SSD [Apple] OS X 10.8.2 ML 13" 2560x1600 Retina [Apple] Backlit Keyboard 
CaseMouse Pad
[Apple] Unibody [Apple] Trackpad 
  hide details  
Reply
post #2 of 7
try using the getcwd (get current working directory) function. here's some C code i whipped together to demonstrate the functionality. getcwd() returns a char*, so just cast the result as a string and you should be good. there's also a way to do it in windows. take a look here http://stackoverflow.com/questions/2203159/is-there-a-c-equivalent-to-getcwd

Code:
#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[]){
        char buf[64];
        size_t size = 64;
        
        printf("argv[0]: %s\n", argv[0]);
        printf("getcwd(): %s\n", getcwd(buf, size));
        return 0;
}

you might also want to use a function like this, so that it will handle error cases.
Quote:
Originally Posted by http://stackoverflow View Post

Code:
std::string get_working_path()
{
    char temp [ PATH_MAX ];

    if ( getcwd(temp, PATH_MAX) == 0) 
        return std::string ( temp );

    int error = errno;

    switch ( error ) {
        // EINVAL can't happen - size argument > 0

        // PATH_MAX includes the terminating nul, 
        // so ERANGE should not be returned

        case EACCES:
            throw std::runtime_error("Access denied");

        case ENOMEM:
            // I'm not sure whether this can happen or not 
            throw std::runtime_error("Insufficient storage");

        default: {
            std::ostringstream str;
            str << "Unrecognised error" << error;
            throw std::runtime_error(str.str());
        }
    }
}

Edited by travesty - 2/22/12 at 9:51am
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #3 of 7
Thread Starter 
Quote:
Originally Posted by travesty View Post

try using the getcwd (get current working directory) function. here's some C code i whipped together to demonstrate the functionality. getcwd() returns a char*, so just cast the result as a string and you should be good. there's also a way to do it in windows. take a look here http://stackoverflow.com/questions/2203159/is-there-a-c-equivalent-to-getcwd
Code:
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *argv[]){
        char buf[64];
        size_t size = 64;
        
        printf("argv[0]: %s\n", argv[0]);
        printf("getcwd(): %s\n", getcwd(buf, size));
        return 0;
}
you might also want to use a function like this, so that it will handle error cases.
Quote:
Originally Posted by http://stackoverflow View Post

Code:
std::string get_working_path()
{
    char temp [ PATH_MAX ];
    if ( getcwd(temp, PATH_MAX) == 0) 
        return std::string ( temp );
    int error = errno;
    switch ( error ) {
        // EINVAL can't happen - size argument > 0
        // PATH_MAX includes the terminating nul, 
        // so ERANGE should not be returned
        case EACCES:
            throw std::runtime_error("Access denied");
        case ENOMEM:
            // I'm not sure whether this can happen or not 
            throw std::runtime_error("Insufficient storage");
        default: {
            std::ostringstream str;
            str << "Unrecognised error" << error;
            throw std::runtime_error(str.str());
        }
    }
}

As noobish as I am, that didn't make very much sense to me confused.gif
PATH_MAX comes as a undeclared definer, and getcwd has an output of 0.
I need some serious help with this one confused.gif
Citra ♥
(15 items)
 
Big Mac
(10 items)
 
 
CPUMotherboardGraphicsRAM
[Intel] Core i7 2600K [Asus] P8Z68-V Pro [Gainward] Geforce 9800 GTX+ [Corsair] Vengeance 16GB @ 1600 
Hard DriveHard DriveCoolingOS
[Kingston] HyperX 120GB SSD [Intel] 520 series 60GB [Corsair] H80 [Canonical] Ubuntu 12.10 
MonitorKeyboardPowerCase
[BenQ] XL2420T [Corsair] Vengeance K60 Performance [OCZ] ZX 850W [CM] Storm Enforcer 
MouseAudioOther
[Razer] Mamba 4G 2012 [Beats] urBeats [NZXT] 24/8/6 pin extensions 
CPUMotherboardGraphicsRAM
[Intel] Core i5-3210M [Apple] Proprietary [Intel] HD 4000 [Hynix] H5TC2G83CFR DDR3L 8GB 
Hard DriveOSMonitorKeyboard
[Samsung] 128GB SSD [Apple] OS X 10.8.2 ML 13" 2560x1600 Retina [Apple] Backlit Keyboard 
CaseMouse Pad
[Apple] Unibody [Apple] Trackpad 
  hide details  
Reply
Citra ♥
(15 items)
 
Big Mac
(10 items)
 
 
CPUMotherboardGraphicsRAM
[Intel] Core i7 2600K [Asus] P8Z68-V Pro [Gainward] Geforce 9800 GTX+ [Corsair] Vengeance 16GB @ 1600 
Hard DriveHard DriveCoolingOS
[Kingston] HyperX 120GB SSD [Intel] 520 series 60GB [Corsair] H80 [Canonical] Ubuntu 12.10 
MonitorKeyboardPowerCase
[BenQ] XL2420T [Corsair] Vengeance K60 Performance [OCZ] ZX 850W [CM] Storm Enforcer 
MouseAudioOther
[Razer] Mamba 4G 2012 [Beats] urBeats [NZXT] 24/8/6 pin extensions 
CPUMotherboardGraphicsRAM
[Intel] Core i5-3210M [Apple] Proprietary [Intel] HD 4000 [Hynix] H5TC2G83CFR DDR3L 8GB 
Hard DriveOSMonitorKeyboard
[Samsung] 128GB SSD [Apple] OS X 10.8.2 ML 13" 2560x1600 Retina [Apple] Backlit Keyboard 
CaseMouse Pad
[Apple] Unibody [Apple] Trackpad 
  hide details  
Reply
post #4 of 7
Quote:
Originally Posted by bruflot View Post

As noobish as I am, that didn't make very much sense to me confused.gif
PATH_MAX comes as a undeclared definer, and getcwd has an output of 0.
I need some serious help with this one confused.gif

PATH_MAX is just a placeholder in that code. it needs to be defined before you can compile. just add this line after your include statements.
Code:
#define PATH_MAX 128
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #5 of 7
Don't restrict yourself by writing cross-platform code if that isn't actually a goal you set for yourself. What platform is this for, and part of what kind of application is it?

If Windows, use GetModuleFileName with the HMODULE Windows passes you when it starts you.

P.S. Do you have to use C++?
Edited by Coma - 3/1/12 at 2:03pm
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
post #6 of 7
Thread Starter 
Quote:
Originally Posted by Coma View Post

Don't restrict yourself by writing cross-platform code if that isn't actually a goal you set for yourself. What platform is this for, and part of what kind of application is it?
If Windows, use GetModuleFileName with the HMODULE Windows passes you when it starts you.
P.S. Do you have to use C++?

Nope, I don't have to use C++, but it's either that or VB.. And in VB I don't feel like I'm going anywhere, so I moved over to c++ for more possibilities.
And yes, it's for Windows.
Citra ♥
(15 items)
 
Big Mac
(10 items)
 
 
CPUMotherboardGraphicsRAM
[Intel] Core i7 2600K [Asus] P8Z68-V Pro [Gainward] Geforce 9800 GTX+ [Corsair] Vengeance 16GB @ 1600 
Hard DriveHard DriveCoolingOS
[Kingston] HyperX 120GB SSD [Intel] 520 series 60GB [Corsair] H80 [Canonical] Ubuntu 12.10 
MonitorKeyboardPowerCase
[BenQ] XL2420T [Corsair] Vengeance K60 Performance [OCZ] ZX 850W [CM] Storm Enforcer 
MouseAudioOther
[Razer] Mamba 4G 2012 [Beats] urBeats [NZXT] 24/8/6 pin extensions 
CPUMotherboardGraphicsRAM
[Intel] Core i5-3210M [Apple] Proprietary [Intel] HD 4000 [Hynix] H5TC2G83CFR DDR3L 8GB 
Hard DriveOSMonitorKeyboard
[Samsung] 128GB SSD [Apple] OS X 10.8.2 ML 13" 2560x1600 Retina [Apple] Backlit Keyboard 
CaseMouse Pad
[Apple] Unibody [Apple] Trackpad 
  hide details  
Reply
Citra ♥
(15 items)
 
Big Mac
(10 items)
 
 
CPUMotherboardGraphicsRAM
[Intel] Core i7 2600K [Asus] P8Z68-V Pro [Gainward] Geforce 9800 GTX+ [Corsair] Vengeance 16GB @ 1600 
Hard DriveHard DriveCoolingOS
[Kingston] HyperX 120GB SSD [Intel] 520 series 60GB [Corsair] H80 [Canonical] Ubuntu 12.10 
MonitorKeyboardPowerCase
[BenQ] XL2420T [Corsair] Vengeance K60 Performance [OCZ] ZX 850W [CM] Storm Enforcer 
MouseAudioOther
[Razer] Mamba 4G 2012 [Beats] urBeats [NZXT] 24/8/6 pin extensions 
CPUMotherboardGraphicsRAM
[Intel] Core i5-3210M [Apple] Proprietary [Intel] HD 4000 [Hynix] H5TC2G83CFR DDR3L 8GB 
Hard DriveOSMonitorKeyboard
[Samsung] 128GB SSD [Apple] OS X 10.8.2 ML 13" 2560x1600 Retina [Apple] Backlit Keyboard 
CaseMouse Pad
[Apple] Unibody [Apple] Trackpad 
  hide details  
Reply
post #7 of 7
GetModuleFileName with a NULL handle will return the name of the executable the process belongs to (it says this on MSDN). If you're running a DLL and want the name of the DLL, you need to pass it the HMODULE you got in DllMain.
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C++ Application path/directory