0

am exploring c++. what i want is to declare the current directory of my project where the .exe located. How to do it like in c# Directory.GetCurrentDirectory +"\myfile\any.dll". Hope somebody would help me. Am searching here but too advanced for me and can't understand their way. Please see my sample code.

    #include<iostream.h>
    #include<stdlib.h>

    using namespace std;

    int main(int argc, char *argv[])
    {
    std::remove("C:\\myfile\\any.dll");

    }

What i want is like this:

    #include<iostream.h>
    #include<stdlib.h>

    using namespace std;

    int main(int argc, char *argv[])
    {
    std::remove(directory_of_my_project + "\\myfile\\any.dll");

    }
itTech
  • 346
  • 1
  • 7
  • 22
  • There's no way for C++ to know the directory of your project. – john May 28 '20 at 06:37
  • i had this big source project of japanese and chinese old game called ran online then why those dll autamatically detected whenever the player transfers the whole file to any drive they want? For your information, the whole game is made on c++ – itTech May 28 '20 at 06:40
  • I think you are confusing the 'directory of your project' with the 'current working directory'. They're not the same thing and C++ can certainly know the latter (see Deep Shah's answer). – john May 28 '20 at 07:05

1 Answers1

1

You can try #include <filesystem>

std::filesystem::remove(std::filesystem::currentPath() + "/myfile.dll" );

Deep Shah
  • 44
  • 8
  • 1
    You should use the overloaded `/` operator to concatenate paths in a system-agnostic way. – Quentin May 28 '20 at 06:54
  • Yeah sorry, I haven't used remove but I think `std::filesystem::remove()` would work. – Deep Shah May 28 '20 at 06:56
  • hi masters, the compiler raised an error fatal error C1083: Cannot open include file: 'filesytem': No such file or directory where can i find and download this library? – itTech May 28 '20 at 07:03
  • Make sure you are spelling it correctly, it looks like you forgot the second s in filesystem. As George mentioned above, you have to be using C++17 for it as well. – Deep Shah May 28 '20 at 07:07
  • filesystem is part of C++17, you might need to enable that in your compiler settings. – john May 28 '20 at 07:07
  • fatal error C1083: Cannot open include file: 'filesystem': No such file or directory still getting an error ok ill find a way how to enable c++17 let me check thank you. – itTech May 28 '20 at 07:13
  • or could you please help me where or how to enable it? send some reference please. thank you – itTech May 28 '20 at 07:15
  • https://stackoverflow.com/questions/41308933/how-to-enable-c17-compiling-in-visual-studio – Deep Shah May 28 '20 at 07:19