BUt I was wondering if anyone knew. My proffesor asked me to write a program concerning viruses and security. I was wondering how to write a program thatwhen ran outputs the code of the program. EX:
the out put should be something like this:
class test
{
public static void main(String[]args)
{
…
anyone know how this could be achieved?
Yep, I’ve written them many times myself. The easiest form of this type of program is a method that processes a String variable and outputs it, with the String containing all lines of the program apart from the String itself (obviously). You read the String line by line, reconstructing the program on System.out. At some point you will have to reconstruct the line containing the String itself, then continue with the rest of the program.
I’d recommend trying to write one yourself before copying one from the Internet, as it’s a fascinating problem.
If you feel the need to cheat ;), follow this link.
Incidentally, it’s generally considered bad form to use external sources to read the program (as in “cat $0” for Shell, or “10 LIST” in BASIC), and rather cheesy to use #defines in C/C++. Absolute unreadability is a must. ;D