rainwater_stillicide

Challenge TRICKY001

8 posts in this topic

In the language of your choice: write a program which will output it's own source code.

0

Share this post


Link to post
Share on other sites

This is my c++ way of doing it...

#include <iostream>

#include <fstream>

using namespace std;

int main()

{

cout<<"This program will output its own sourcecode... (Hopfully)"<<endl;

ifstream reader;

string line;

reader.open("hello.cpp");

if (reader.is_open())

{

while (! reader.eof() )

{

getline (reader,line);

cout << line << endl;

}

reader.close();

}

else cout << "Unable to open file";

}

Not sure if this is the best way, but it works ^_^

0

Share this post


Link to post
Share on other sites

Nice. Ambitious hacker solution.

Can anyone write a version that doesn't need to read from it's own source file?

0

Share this post


Link to post
Share on other sites

in python:

filename = "selfsrc.py" # or whatever you name the script

filename2 = "output.txt"

file1 = open(filename, "r")

file2 = open(filename2, "w")

txt = file1.read()

file2.write(txt)

file1.close

file2.close

it outputs to file, but you could output to shell instead, if you wanted

0

Share this post


Link to post
Share on other sites

In the language of your choice: write a program which will output it's own source code.

Question: Does the outputted source code have to be able to then be compiled and repeat the objective? I can get the printing of the sourcecode to work, but I lose all formatting.

See my answer:


#include <stdio.h>

char source[] = "#include <stdio.h>"
"char source[] = \"%s\";"
"int main()"
"{"
" printf( source, source );"
" return 0;"
"}";

int main()
{
printf( source, source );
return 0;
}

output:


(10:18 PM Tue Mar 09)
(w@w.domain1)-(~/dev/C/Projects/BinRevChallenges)>./tricky001
#include <stdio.h>char source[] = "#include <stdio.h>char source[] = "%s";int main(){ printf( source, source ); return 0;}";int main(){ printf( source, source ); return 0;}

Edited by FLW_FTW
0

Share this post


Link to post
Share on other sites

In the language of your choice: write a program which will output it's own source code.

Question: Does the outputted source code have to be able to then be compiled and repeat the objective? I can get the printing of the sourcecode to work, but I lose all formatting.

See my answer:


#include <stdio.h>

char source[] = "#include <stdio.h>"
"char source[] = \"%s\";"
"int main()"
"{"
" printf( source, source );"
" return 0;"
"}";

int main()
{
printf( source, source );
return 0;
}

output:


(10:18 PM Tue Mar 09)
(w@w.domain1)-(~/dev/C/Projects/BinRevChallenges)>./tricky001
#include <stdio.h>char source[] = "#include <stdio.h>char source[] = "%s";int main(){ printf( source, source ); return 0;}";int main(){ printf( source, source ); return 0;}

This is a good attempt and is along the right lines. Ideally the output would be identical to the source file (there's nothing stopping you smooshing the original source file onto one line if you like).

The problem with this solution isn't the formatting though (C ignores whitespace generally so even if a program is all on one line it'll still compile). it's that inside the strings the '\'s you use to escape \"%s\" aren't repeated and that you some quotation marks are lost etc...

Keep working at it, it's close to a full solution.

0

Share this post


Link to post
Share on other sites

In Python:

(saved as 'file.py')


#!/usr/bin/env python
print(open('file.py','r').read())

OR if you want to do it the PROPER way:

x='x=%s;x%%`x`';x%`x`

0

Share this post


Link to post
Share on other sites

In Perl:


#!/usr/bin/perl
open FILE, "<$0" or die $!;
while (<FILE>) {print $_;}

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now