Search code examples
c++visual-studio-2013directory

How to return the directory of the cpp file?


I am trying to return the path of the cpp file I am running. Does anyone know a method or a way to implement this? For example lets say I have this file test.cpp at the path in my computer "C:\Programming\Visual Studio\Test\Test\test.cpp".

Is there a way to get this path without manually typing it? I am trying to determine a method of using c++ to return this path.

For my ftp program I need to get the list of .txt, .pdf, .etc files, which are located at the same path as the .cpp file. This is why I want the .cpp path and not the .exe path.

Any suggestions?


Solution

  • What about this??

    #include<iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string file_path = __FILE__;
        string dir_path = file_path.substr(0, file_path.rfind("\\"));
        cout<<dir_path<<endl;
    
        return 0;
    }