12

I want to be able to get the current source file path.

string txt_file = CURRENT_FILE_PATH +"../../txt_files/first.txt";
inFile.open(txt_file .c_str());

Is there a way to get the CURRENT_FILE_PATH ? I don't mean the executable path. I mean the current location of the source file that the code is running from.

Thanks a lot, Giora.

gioravered
  • 1,758
  • 3
  • 19
  • 30
  • It seem unclear what you are asking for. The current working directory is given by `getcwd()` and the path of the source file is accessible through the `__FILE__` macro. – Robert Jørgensgaard Engdahl Dec 05 '13 at 07:48
  • Your asking about the module mean the executable so you need the working directory this is the answer for it http://stackoverflow.com/questions/143174/how-do-i-get-the-directory-that-a-program-is-running-from – ahmedsafan86 Dec 05 '13 at 07:51
  • Maybe the word: Module is not correct. I want the path of the source file. __FILE__ is giving me the relative path. I want the full path. – gioravered Dec 05 '13 at 07:55
  • For Windows, see [this question](http://stackoverflow.com/q/8354422/440558), for Linux/OSX see [this question](http://stackoverflow.com/questions/2341808/how-to-get-the-absolute-path-for-a-given-relative-path-programmatically-in-linux). – Some programmer dude Dec 05 '13 at 07:57
  • The answer there is using realpath() function. I've tried that, but it requires a relative path to begin with. I don't have it. I "know nothing" about the source file location. – gioravered Dec 05 '13 at 08:29

2 Answers2

9

C++20 source_location::file_name

We now have another way besides __FILE__, without using the old C preprocessor: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1208r5.pdf

The documentation simply says:

constexpr const char* file_name() const noexcept;

5 Returns: The presumed name of the current source file (14.2) represented by this object as an NTBS.

where NTBS means "Null Terminated Byte String".

I'll give it a try when support arrives to GCC, GCC 9.1.0 with g++-9 -std=c++2a still doesn't support it.

https://en.cppreference.com/w/cpp/utility/source_location claims usage will be like:

#include <iostream>
#include <string_view>
#include <source_location>

void log(std::string_view message,
         const std::source_location& location std::source_location::current()
) {
    std::cout << "info:"
              << location.file_name() << ":"
              << location.line() << ":"
              << location.function_name() << " "
              << message << '\n';
}

int main() {
    log("Hello world!");
}

Possible output:

info:main.cpp:16:main Hello world!
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
8

The path used to compile the source file is accessible through the standard C macro __FILE__ (see http://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html)

If you give an absolute path as input to your compiler (at least for gcc) __FILE__ will hold the absolute path of the file, and vice versa for relative paths. Other compilers may differ slightly.

If you are using GNU Make and you list your source files in the variable SOURCE_FILES like so:

SOURCE_FILES := src/file1.cpp src/file2.cpp ...

you can make sure the files are given by their absolute path like so:

SOURCE_FILES := $(abspath src/file1.cpp src/file2.cpp ...)