Toni Pop Toni Pop - 2 months ago 14
C++ Question

How to i execute a cpp file within a cpp file?

I'm making this little text rpg game and i want to display this ascii as a death screen. I have made a c++ file that has that (i found the code online and i didn't want to put it in the main code just cause it'd be too long to place that whenever you die, it reads a .txt file with the ascii) and i need a way to execute it from the main cpp file when i run it. Is there a way?
I have searched the web, but nothing i could clearly understand came out.

here's the code for the ascii if it helps:

#include <iostream>
#include <fstream>
#include <string>

std::string getFileContents (std::ifstream&); /*Gets filecontents*/

int main(int argc, char *argv[])

std::ifstream Reader ("ded.txt"); //Open file

std::string Art = getFileContents (Reader); //Get file

std::cout << Art << std::endl; //Print it to the screen

Reader.close (); //Close file

return 0;

std::string getFileContents (std::ifstream& File)
std::string Lines = ""; //All lines

if (File) //Check if everything is good
while (File.good ())
std::string TempLine; //Temp line
std::getline (File , TempLine); //Get temp line
TempLine += "\n"; //Add newline character

Lines += TempLine; //Add newline
return Lines;
else //Return error
return "ERROR File does not exist.";


There is no need to make a new program to print this on the screen...

And that's for a simple reason, it will actually print it on a different window and that's not what you want, right?

Another misunderstanding you have is that you don't execute .cpp
files. .cpp files contain source code that you have to compile in order to execute them. And again this is not how stuff works in C++.

So, to the solution.

On your programs code make a new function, let's say PrintDeathScreen. You have two options. Read the ASCII from the file as you would do (but I don't really recomend that) or hard-code it into a variable and then just print it on the screen. This will save you from the trouble to open and read the file.