print the content of your program's source file to console, without user inputting the file name or giving it in source.
the main problem here is getting the file name. from executable file name we get cpp file name & the rest is easy.
the following solution is windows only.
Since C++20 use std::source_location to get info about the source file. Consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <source_location>
#include <string>
#include <fstream>
int main() {
constauto prgnam { std::source_location::current().file_name()};
std::cout << prgnam << "\n\n";
if (std::ifstream ifs { prgnam }; ifs)
for (std::string line; std::getline(ifs, line); std::cout << line << '\n');
else
std::cout << "Whoops - the program has disappeared!\n";
}
which displays the source code for itself.
the main problem here is getting the file name. from executable file name we get cpp file name
Note that there is no automatic correlation between the name of the .exe and the name(s) and the source files used. Just obtaining the name of the running .exe doesn't give you any useful info about the name(s) of the source programs used to produce.
If you are indeed being asked to display source file(s) from an .exe then some assumptions need to be stated (eg if exe name is test.exe then the source file is test.cpp and is in the same folder etc).
This will obtain just the name of the program as it is typed to invoke the program (which may or may be fully qualified and may or may not include a file . extension).
If you want a fully qualified name of the .exe being run then AFAIK there is no platform independent direct solution. For Windows, you use GetModuleFileName(). For OS independence you can combine using argv[0] and extracting just the name (which may be os dependent??) together with std::filesystem::current_path() to get a fully qualified name of the .exe.