print the content of your program's source file to console, without user inputting the file name or giving it in source.

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <windows.h>///

std::string exepath(){
  char result[ MAX_PATH ];
  return std::string( result, GetModuleFileName( NULL, result, MAX_PATH ));
}

main(){
	std::string s =  exepath();
	std::cout<< s << "\n";
}


is there a platform independent solution?
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <filesystem>
#include <fstream>

int main(){
	std::string s= std::filesystem::current_path().string();
	std::cout <<"current path = " << std::filesystem::current_path() <<"\n";
	std::cout << "s = " << s << "\n"; // path without file name

}
print the content of your program's source file to console,

Why do you want the path of the .exe when you need the program source file?
The name of the .cpp file you can get with __FILE__
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() {
	const auto 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).

1
2
3
4
5
#include <iostream>

int main(int, char *argv[]) {
	std::cout << argv[0] << '\n';
}


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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
   ifstream in( __FILE__ );
   string str;
   getline( in, str, '\0' );
   cout << str;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// main.cpp
// Platform independent solution
// Compile with /std:c++20

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;


int main(int argc, char* argv[])
{
    // Get executable name
    fs::path executable_path = argv[0];
    const std::string executable_name = executable_path.stem().string();
    
    // Path to source file
    fs::path file_path = executable_name + ".cpp";
    fs::path source_file = executable_path.parent_path().append(file_path.string());

    if (fs::exists(file_path))
    {
	// TODO: Read file here
	std::cout << "Source file path is " << source_file;
    }
    else
    {
	std::cerr << "Unable to locate file " + source_file.string();
	return EXIT_FAILURE;
    }
}
Last edited on
They're called quines if you're interested.
https://en.wikipedia.org/wiki/Quine_(computing)
restating JL's solution from that post,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>

const std::string text = R"quine(
        const std::string prefix = "#include <iostream>\n"
                                   "#include <string>\n\n"
                                   "const std::string text = R\"quine(" ;
        const std::string suffix = ")quine\";\n\n";
        const std::string quine = prefix + text + suffix + text ;
            //         _
            //        / )
            //       / /
            //      / /               /)
            //     / /     .-```-.   / ^`-.
            //     \ \    /       \_/  (|) `o
            //      \ \  /   .-.   \\ _  ,--'
            //       \ \/   /   )   \( `^^^
            //        \   \/    (    )
            //         \   )     )  /
            //          ) /__    | (__
            //         (___)))   (__)))
            //
        int main() { std::cout << quine ; }

)quine";


        const std::string prefix = "#include <iostream>\n"
                                   "#include <string>\n\n"
                                   "const std::string text = R\"quine(" ;
        const std::string suffix = ")quine\";\n\n";
        const std::string quine = prefix + text + suffix + text ;
            //         _
            //        / )
            //       / /
            //      / /               /)
            //     / /     .-```-.   / ^`-.
            //     \ \    /       \_/  (|) `o
            //      \ \  /   .-.   \\ _  ,--'
            //       \ \/   /   )   \( `^^^
            //        \   \/    (    )
            //         \   )     )  /
            //          ) /__    | (__
            //         (___)))   (__)))
            //
        int main() { std::cout << quine ; }

output:
#include <iostream>
#include <string>

const std::string text = R"quine(
        const std::string prefix = "#include <iostream>\n"
                                   "#include <string>\n\n"
                                   "const std::string text = R\"quine(" ;
        const std::string suffix = ")quine\";\n\n";
        const std::string quine = prefix + text + suffix + text ;
            //         _
            //        / )
            //       / /
            //      / /               /)
            //     / /     .-```-.   / ^`-.
            //     \ \    /       \_/  (|) `o
            //      \ \  /   .-.   \\ _  ,--'
            //       \ \/   /   )   \( `^^^
            //        \   \/    (    )
            //         \   )     )  /
            //          ) /__    | (__
            //         (___)))   (__)))
            //
        int main() { std::cout << quine ; }

)quine";


        const std::string prefix = "#include <iostream>\n"
                                   "#include <string>\n\n"
                                   "const std::string text = R\"quine(" ;
        const std::string suffix = ")quine\";\n\n";
        const std::string quine = prefix + text + suffix + text ;
            //         _
            //        / )
            //       / /
            //      / /               /)
            //     / /     .-```-.   / ^`-.
            //     \ \    /       \_/  (|) `o
            //      \ \  /   .-.   \\ _  ,--'
            //       \ \/   /   )   \( `^^^
            //        \   \/    (    )
            //         \   )     )  /
            //          ) /__    | (__
            //         (___)))   (__)))
            //
        int main() { std::cout << quine ; }

 
Normal program termination. Exit status: 0


the program felt weird to me. then i found why. it prints 1st squirrel twice. doesn't print 2nd squirrel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

const std::string text = R"quine(
        const std::string prefix = "#include <iostream>\n"
                                   "#include <string>\n\n"
                                   "const std::string text = R\"quine(" ;
        const std::string suffix = ")quine\";\n\n";
        const std::string quine = prefix + text + suffix + text ;
            //comment. print me!
        int main() { std::cout << quine ; }

)quine";


        const std::string prefix = "#include <iostream>\n"
                                   "#include <string>\n\n"
                                   "const std::string text = R\"quine(" ;
        const std::string suffix = ")quine\";\n\n";
        const std::string quine = prefix + text + suffix + text ;
            //comment. don't print me!
        int main() { std::cout << quine ; }


output:
#include <iostream>
#include <string>

const std::string text = R"quine(
        const std::string prefix = "#include <iostream>\n"
                                   "#include <string>\n\n"
                                   "const std::string text = R\"quine(" ;
        const std::string suffix = ")quine\";\n\n";
        const std::string quine = prefix + text + suffix + text ;
            //comment. print me!
        int main() { std::cout << quine ; }

)quine";


        const std::string prefix = "#include <iostream>\n"
                                   "#include <string>\n\n"
                                   "const std::string text = R\"quine(" ;
        const std::string suffix = ")quine\";\n\n";
        const std::string quine = prefix + text + suffix + text ;
            //comment. print me!
        int main() { std::cout << quine ; }

 
Normal program termination. Exit status: 0

is there scope of (philosophical) debate(about is it a quine) as technically it doesn't print 2nd squirrel?
is there scope of (philosophical) debate


There's no way that is a squirrel!
More like a Cat On A Hot Tin Roof
Looks like a cat after having it's temperature taken at the vet. *OUCH! HOWL!*
If it had ^^ instead of ^^^ teeth I would a vampire but I hope JLBorges don't take this joke seriously lol :D
Topic archived. No new replies allowed.