473,803 Members | 3,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


Is it possible to write a program which would print out it's own
source code, using C++?
Jan 23 '08
21 3592
Victor Bazarov wrote:
Kira Yamato wrote:
>On 2008-01-23 10:24:30 -0500, anon <an**@no.nosaid :
>>mi******@gmail. com wrote:
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:
deep, philosophically ... lol.

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'
:(
Jan 24 '08 #11
On Jan 23, 10:41 pm, "Bo Persson" <b...@gmb.dkwro te:
Victor Bazarov wrote:
Kira Yamato wrote:
On 2008-01-23 10:24:30 -0500, anon <a...@no.nosaid :
>mikha...@gmail .com wrote:
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:
deep, philosophically ... lol.
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.
For what system? In a hosted environment, a C++ program must
contain a function main somewhere.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 24 '08 #12
kwikius <an**@servocomm .freeserve.co.u kwrites:
>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 :-)
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?

--
__Pascal Bourguignon__
Jan 24 '08 #13
On Jan 23, 6:15*am, mikha...@gmail. com wrote:
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
Jan 24 '08 #14
red floyd wrote:
>
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;
}
Non-portably and as a first thought I can think of this for my system:
#include <cstdlib>

int main()
{
std::system("ca t main.cc");
}
It outputs:

[john@localhost src]$ ./foobar-cpp
#include <cstdlib>

int main()
{
std::system("ca t main.cc");
}

[john@localhost src]$
Jan 24 '08 #15
Ioannis Vranos wrote:
>
Non-portably and as a first thought I can think of this for my system:
#include <cstdlib>

int main()
{
std::system("ca t main.cc");
}
It outputs:

[john@localhost src]$ ./foobar-cpp
#include <cstdlib>

int main()
{
std::system("ca t main.cc");
}

[john@localhost src]$

This can be improved my specifying int main(int argc, char **argv),
taking as an argument the source file. The program requirements doesn't
specify that arguments are prohibited.

But regarding portability, that isn't portable too.
Jan 24 '08 #16
mi******@gmail. com a écrit :
Is it possible to write a program which would print out it's own
source code, using C++?
I am not very confident with this but I think there are debugging
options or formats that embedded the source code in the executable... If
a programm is able to display these informations it surely can be
executed againsts itself.
Jan 24 '08 #17
On Jan 24, 2:03*pm, Pascal Bourguignon <p...@informati mago.comwrote:
Puppet_Sock <puppet_s...@ho tmail.comwrites :
On Jan 23, 6:15*am, mikha...@gmail. com wrote:
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.

Then it wouldn't be the program that would print out its own source
code, it would be the compiler.
You know, I never get the memos. When did the memo
go around that we were all supposed to be idiots?

Me: The trick is to get the compiler to do it.
You: Then it would be the compiler doing it.
Me: (Does my Benny Hill imitation, slapping you
repeatedly on the back of the head like
B.H. did that little bald guy.)
Socks
Jan 24 '08 #18
I cannot see why it would be impossible to write a decompiler that was
carefully coded to decompile itself to its own source.
Is there some computation theory that I am missing?
Is it possible to write a program which would print out it's own
source code, using C++?

Jan 24 '08 #19
On Jan 24, 4:16*pm, p...@informatim ago.com (Pascal J. Bourguignon)
wrote:
kwikius <a...@servocomm .freeserve.co.u kwrites:
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 :-)

But it's not what's specified.
From the O.P....

"Is it possible to write a program which would print out it's own
source code, using C++? "
As explained above I've even thrown in some very useful extra
functionality!! :-)

regards
Andy Little
Jan 25 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
14170
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip Smarty-2.6.7.tar.gz on a system running WindowsXP SP2. Apache and PHP tested out fine. After adding Smarty, I ran the following http://localhost/testphp.php
1
2365
by: Hung Jung Lu | last post by:
Hi, I have been looking into AOP (Aspect-Oriented Programming) for sometime, now. I frankly don't like the syntax of any of the approaches I have seen so far. I am kind playing around with some ideas, and maybe write up an article later. AOP is not just buzzword. It's not just callback, it's not just Ruby's MixIn, it's not just Python's metaclass, it's not just C++'s template. AOP can be implemented/considered as a subset of...
5
1476
by: aleksander.helgaker | last post by:
I've completely rewritten a calculator I wrote to help me learn Python. After someone told me about the def command I reliesed that I could make the program much better, but there is a very anoying problem which ocours when I run the program. Here is the code <code> # IMPOSRTS # import sys import os
5
1784
by: Henry Jordon | last post by:
hello I was wondering if someone could help me get a main going on this project I've completed the header file that the professor started us on but not really sure how to get the main going. If someone could please give me some pointers it would greatly be appreciated. Again thanks for the help. Henry headerfile:
4
7066
by: yinglcs | last post by:
I am trying to debug a C++ program in GDB on linux. I want to dump out the content of the "this" object, Here is what I get: (gdb) print *this $2 = {path = <incomplete type>} My question is why I don't see the content of 'path'? It said '<incomplete type>'. In the code, path is:
9
7880
by: sandy | last post by:
Hey guys, I know this is silly, but I just wanted to take some break from work. I was just wodering if its possible to write a c++ program that can print "iloveyou" in shape of 'heart' when executed. The impotant thing is that, the program should not show any kind of words related to it, i mean the program should not be understandable at the very first sight the user goes through the code. It will be gr8 if something more creative is...
12
2857
by: DannyB | last post by:
I'm just learning Python. I've created a simple coin flipper program - here is the code: #Coin flipper import random heads = 0 tails = 0 counter = 0
2
19355
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how we set about doing that. Every program starts with a specification, this may be a several hundred page document from your latest client or one small paragraph from your professor and pretty much anything in-between. The specification is very...
4
5327
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file (Sorts.java): import java.util.*; /**
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10309
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.