A program which would print out it's own source code - possible ornot?

M

mikhal80

Is it possible to write a program which would print out it's own
source code, using C++?
 
P

Pascal Bourguignon

Is it possible to write a program which would print out it's own
source code, using C++?

Yes, that's trivial.

--
__Pascal Bourguignon__ http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
 
P

Pascal Bourguignon

Victor Bazarov said:
It probably is. But my dumb compiler complains that there is no
'main' in my program...

Ok, so you need at least a main.

int main(){
// add something here...
return 0;
}


--
__Pascal Bourguignon__ http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
 
V

Victor Bazarov

Pascal said:
Ok, so you need at least a main.

int main(){
// add something here...
return 0;
}

Weird. This one doesn't print its own source code. Why?

V
 
K

kwikius

Is it possible to write a program which would print out it's own
source code, using C++?

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);

}

regards
Andy Little
 
R

red floyd

Victor said:
Weird. This one doesn't print its own source code. Why?

V

Allow me to correct Pascal's code:

int main()
{
// add something here to print its own source.
// this is left as an exercise to the reader
return 0;
}
 
A

anon

Victor said:
It probably is. But my dumb compiler complains that there is no
'main' in my program...

You need to tweak it a bit (different compiler options). My linker
complains:
(.text+0x18): undefined reference to `main'
:(
 
J

James Kanze

But other compilers actually once compiled this, ran it, and produced
an identical empty output.

For what system? In a hosted environment, a C++ program must
contain a function main somewhere.
 
J

James Kanze

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...

Why shouldn't it be?
#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);
}

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.
 
K

kwikius

Why shouldn't it be?

I think I had problems once upon a time. It may have been ambiguity
with function declarations, not being in a for statement, which was
the problem before though.

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.

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 :)

IIRC the file macro gives the full path with VC++ but not gcc.

regards
Andy Little
 
P

Pascal J. Bourguignon

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 :)

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?
 
P

Puppet_Sock

Is it possible to write a program which would print out it's own
source code, using C++?

That's old. The trick is to get the compiler
to spit out the entire source code from
messages produced during compilation.
Socks
 
A

Alf P. Steinbach

* Pascal J. Bourguignon:
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?

quine.

Cheers, & hth.,

- Alf
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top