M
mikhal80
Is it possible to write a program which would print out it's own
source code, using C++?
source code, using C++?
Is it possible to write a program which would print out it's own
source code, using C++?
Is it possible to write a program which would print out it's own
source code, using C++?
Is it possible to write a program which would print out it's own
source code, using C++?
yes, here is one example for free:
Kira said:deep, philosophically... lol.
Is it possible to write a program which would print out it's own
source code, using C++?
Victor Bazarov said:It probably is. But my dumb compiler complains that there is no
'main' in my program...
Pascal said:Ok, so you need at least a main.
int main(){
// add something here...
return 0;
}
Victor said:It probably is. But my dumb compiler complains that there is no
'main' in my program...
Bo said:But other compilers actually once compiled this, ran it, and produced
an identical empty output.
This entry in the 1994 Obfuscated C contest won its class
http://www.ioccc.org/1994/smr.c
http://www.ioccc.org/1994/smr.hint
and changed the category into "Smallest, non-empty, self-replicating
program".
Is it possible to write a program which would print out it's own
source code, using C++?
Victor said:Weird. This one doesn't print its own source code. Why?
V
Victor said:It probably is. But my dumb compiler complains that there is no
'main' in my program...
But other compilers actually once compiled this, ran it, and produced
an identical empty output.
On Jan 23, 11:15 am, (e-mail address removed) wrote:
Heres my effort FWIW. (BTW, if its a homework assigmment then I
wouldnt recommend handing the following in).
Also though it compiles in gcc 3.4 I don't know that the fstream
declaration in the for loop is actually legal. I have a funny idea it
isnt for some reason...
#include <fstream>
#include <string>
#include <cassert>
#include <iostream>
template <typename T1, typename T2>
inline
void apply(T1* f,T2 & out)
{
std::string str;
for(
std::ifstream in(f);
getline(in,str);
out << str <<'\n'
){}
}
int main()
{
apply(__FILE__,std::cout);
}
Why shouldn't it be?
When I tried it, it worked in the directory where I compiled it,
but when I copied it to ~/bin, and invoked it from there, it
output an earlier suggestion---the one without a main.
kwikius said:Yes! It was hard solving that one as well but I figured it eventually.
Isnt that even more useful than the original application?... its
multipurpose
Is it possible to write a program which would print out it's own
source code, using C++?
But it's not what's specified.
So how could you write a program that writes a copy of its source,
without relying on run-time external files other than the executable,
since they may not be available?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.