I need to make a program that reads the program its self and echo prints it to the screen. I appreciate any help. For example if the program was:

<iostream>
using namespace std;

int main (){

//This is my program


}
return 0;

My output would be this printed on the screen.

<iostream>
using namespace std;

int main (){

//This is my program


}
return 0;

Recommended Answers

All 10 Replies

Hey (I usually get shouted at for giving code) But wouldn't you just put it in a cout statement? For example:

string code;
code = "// the code";
cout << code;

I think that's what you mean. Sorry if this is no help to you.

Thanks for the quick reply, but let me clarify what i need a little better. Lets say my source file is called program.cpp. In my code i will be using something like the following except I want to print to screen the entire contents of program.cpp and not just do a count of a certain character.

#include <iostream>
#include <fstream>

using namespace std;

const int circle = 15;


int main(){

	ifstream ifile;

	ifile.open(" program.cpp");
	if(! ifile){
		cout << "error open the file" << endl;
		return 1;
	}

	int count = 0;
	char c;

	ifile.get(c);
	while(ifile){

		if(c == ';'){
			count ++;
		}
		
		ifile.get(c);
	}
	

	cout << "the number of semicolon in the file is: " << count << endl;
	return 0;
}

Still can't wrap my mind around this idea. Can anyone help?

The easiest way to do this is to open the .cpp file and display it using cout. As a hint for a program displaying its own code, there is a preprocessor macro called __FILE__ that evaluates to a string with the name of the current file. This is the simplest type of quine that I have been able to figure out. ;)

The trick is to remember that it is not the code that is running, it is a compiled executable that was copied into memory. All of the files used to create that executable, and even the executable itself, are all just regular files that you can open and read.

Is there any way to do this using the ifile.open from the code I posted above. Instead of searching the file for a semicolon, I want to read the contents of the entire file and then display it on the screen. I know they're are easier ways, I'd like to just figure it out this way. :-P Thanks for your help so far.

Just print the file line by line. Not all lines in C++ end with a semicolon, but they do end with a line break. ;)

So something along the lines of this?

#include <iostream>
#include <fstream>

using namespace std;

int main(){

	ifstream ifile;

	ifile.open(" echo.cpp");
	if(! ifile){
		cout << "error open the file" << endl;
		return 1;
	}

while (! ifile.eof() )
    {
      getline (ifile,line);
      cout << line << endl;
    }
    ifile.close();

}

I just don't know the correct code to read it line by line. I know how to do it character by character, but not line by line. So the above was just guesswork and doesn't work based on what I found on the net.

So something along the lines of this?

Close.

using namespace std;

The prevailing opinion is that a using directive should not be used at global scope. You can put this same line inside main() and have the same effect without making the std namespace visible everywhere else too. In a little program like this it is not a big deal, but name collisions become a problem very quickly as your programs grow.

cout << "error open the file" << endl;

While there is nothing wrong with this, it is not informative. Why did the file not open? I am not 100% sure that this is portable with fstreams, but I have always used perror() to get more details. You can find perror() in the <cstdio> header.

return 1;

Technically this is not portable because you do not know that an error code of 1 means the same thing on every system. For a generic error return it is more portable to return EXIT_FAILURE from the <cstdlib> header:

if (!ifile)
{
    perror("Error opening file");
    return EXIT_FAILURE;
}
while (! ifile.eof() )

This is not a good idea because eof() does not work in the same order that you expect. eof() only returns true after a failed read, which means that in some cases you will loop over the last line in the file twice. getline() returns a reference to the stream, and you can use that as a condition because reading to the end of the file sets an error state:

while (getline(ifile, line)) cout << line << '\n';

You also need to include the <string> header for string objects and declare a string object called line:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>

int main()
{
    using namespace std;

    ifstream ifile("echo.cpp");

    if (!ifile)
    {
        perror("Error opening file");
        return EXIT_FAILURE;
    }

    string line;

    while (getline(ifile, line)) cout << line << '\n';
}

Awesome thank you! I learned a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.