13

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.

5
  • 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. Commented Dec 5, 2013 at 7:48
  • Your asking about the module mean the executable so you need the working directory this is the answer for it stackoverflow.com/questions/143174/… Commented Dec 5, 2013 at 7: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. Commented Dec 5, 2013 at 7:55
  • For Windows, see this question, for Linux/OSX see this question. Commented Dec 5, 2013 at 7: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. Commented Dec 5, 2013 at 8:29

2 Answers 2

11

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!
Sign up to request clarification or add additional context in comments.

1 Comment

You said this is for C++20, how would one do this if they're using C++11?
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 ...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.