Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,988 questions

51,933 answers

573 users

How to get the current directory in C++

2 Answers

0 votes
#include <direct.h>
#include <iostream>

#define PATH_MAX 512

int main() {
    char buffer[PATH_MAX];

    if (_getcwd(buffer, sizeof(buffer)) != nullptr) {
        std::cout << "Current working dir: " << buffer << "\n";
    }
    else {
        std::cerr << "Error getting current directory.\n";
    }
}





/*
run:

Current working dir: C:\Projects

*/

 



answered Jun 29, 2024 by avibootz
0 votes
#include <windows.h>
#include <iostream>

std::wstring ExePath() {
    TCHAR buffer[MAX_PATH] = { 0 };
    
    GetModuleFileName(NULL, buffer, MAX_PATH);
    
    std::wstring::size_type pos = std::wstring(buffer).find_last_of(L"\\/");

    return std::wstring(buffer).substr(0, pos);
}

int main() {
    std::wcout << "My directory is " << ExePath() << "\n";
}





/*
run:

My directory is C:\Project\x64\Debug

*/

 



answered Jun 29, 2024 by avibootz

Related questions

2 answers 105 views
1 answer 91 views
91 views asked Jun 29, 2024 by avibootz
2 answers 344 views
1 answer 106 views
4 answers 159 views
159 views asked Jun 29, 2024 by avibootz
...