Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 534
VMA92 Avatar asked Mar 18 '23 08:03

VMA92


1 Answers

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;
}
like image 106
SHR Avatar answered Mar 24 '23 21:03

SHR