473,383 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

To Print the source program as output

How to Print the complete source program as output of the program?

Aug 3 '07 #1
15 2152
vi************@gmail.com wrote:
How to Print the complete source program as output of the program?
Please google for "self-printing program". It is a fun problem to
solve, but IIRC, it doesn't have any portable solution.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 3 '07 #2

Victor Bazarov <v.********@comAcast.netwrote in
<f8**********@news.datemas.de>:
vi************@gmail.com wrote:
>How to Print the complete source program as output of
the program?

Please google for "self-printing program".
Or, better yet, "quine".
It is a fun problem to solve, but IIRC, it doesn't have
any portable solution.
Why not? What am I missing? Some of the C++ quines I've
found in a couple of minutes of googling (e.g., a David
Rogers' one on Gary Thompson's page) seem to be easily
adapted to be standard-compliant and reasonably portable.

--
....the pleasure of obedience is pretty thin compared with
the pleasure of hearing a rotten tomato hit someone in the
rear end. -- Garrison Keillor
Aug 3 '07 #3
Pavel Lepin wrote:
Victor Bazarov <v.********@comAcast.netwrote in
<f8**********@news.datemas.de>:
>vi************@gmail.com wrote:
>>How to Print the complete source program as output of
the program?

Please google for "self-printing program".

Or, better yet, "quine".
>It is a fun problem to solve, but IIRC, it doesn't have
any portable solution.

Why not? What am I missing? Some of the C++ quines I've
found in a couple of minutes of googling (e.g., a David
Rogers' one on Gary Thompson's page) seem to be easily
adapted to be standard-compliant and reasonably portable.
Most of them involve ASCII coding. That's not portable.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 3 '07 #4
Victor Bazarov wrote:
Most of them involve ASCII coding. That's not portable.
How many systems can you mention which are used today and which
do not use ASCII coding?

Heck, *no* C++ source code is portable to systems which do not
use ASCII coding because the source code itself uses ASCII coding.
Aug 3 '07 #5
On 2007-08-04 00:48, Juha Nieminen wrote:
Victor Bazarov wrote:
>Most of them involve ASCII coding. That's not portable.

How many systems can you mention which are used today and which
do not use ASCII coding?
I believe that IBM still uses EBCDIC on their z series machines.
Heck, *no* C++ source code is portable to systems which do not
use ASCII coding because the source code itself uses ASCII coding.
Not unless it's not written on a machine which uses ASCII, besides I
don't think the C++ standard defines how the source should be encoded,
only which characters that can be expected to be recognised.

--
Erik Wikström
Aug 3 '07 #6
"Juha Nieminen" <no****@thanks.invalidwrote in message
news:46***********************@news.song.fi...
Victor Bazarov wrote:
>Most of them involve ASCII coding. That's not portable.

How many systems can you mention which are used today and which
do not use ASCII coding?

Heck, *no* C++ source code is portable to systems which do not
use ASCII coding because the source code itself uses ASCII coding.
Sure it is. Take, for example, an AS/400 which uses EBCDIC. You can
download source code, which was encoded in ASCII, which the AS/400
communications will automatically convert to EBCDIC, giving you the same
source code but with different encoding.

What source code is encoded in isn't actually part of the source code, just
how it's transmitted/stored.
Aug 4 '07 #7
In article <f8**********@news.datemas.de>, v.********@comAcast.net
says...
vi************@gmail.com wrote:
How to Print the complete source program as output of the program?

Please google for "self-printing program". It is a fun problem to
solve, but IIRC, it doesn't have any portable solution.
I suppose that depends a bit on how you define "portable". Here's my
mildly edited version of David Roger's attempt:

---------------Start--------------------
#include <iostream>
#define Q(T) std::cout << T << "(" << #T << ");}";

int main()
{Q("#include <iostream>\n#define Q(T) std::cout << T << \"(\" << #T <<
\");}\";\n\nint main()\n{Q");}
---------------Finish-------------------

[Note: everything from the "{Q" to the end should be one line.]

I think this qualifies as "portable" the way most people use the term. I
believe it should work equally well with EBCDIC or ASCII.

In theory, it does have a couple of minor shortcomings: 1) the final
line in the output isn't terminated by a newline. 2) its source includes
characters not present in the ISO 646 character set, so it couldn't be
entered on an ISO 646 terminal (but I definitely do NOT want to get into
the ugliness of trigraphs...)

Other than that, you could theoretically rule it out because it writes
to standard output in translated mode, which allows an implementation
defined mapping between what you write and what really appears as the
output. Likewise, there's an implementation defined mapping from the
source character set to the execution character set. On a purely
theoretical basis, these allow it to produce essentially anything (or
nothing at all) as output.

OTOH, by most of these criteria virtually NO code is portable!

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 4 '07 #8
On Aug 3, 4:49 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Pavel Lepin wrote:
Victor Bazarov <v.Abaza...@comAcast.netwrote in
<f8vd4n$v7...@news.datemas.de>:
vinay.khank...@gmail.com wrote:
How to Print the complete source program as output of
the program?
Please google for "self-printing program".
Or, better yet, "quine".
It is a fun problem to solve, but IIRC, it doesn't have
any portable solution.
Why not? What am I missing? Some of the C++ quines I've
found in a couple of minutes of googling (e.g., a David
Rogers' one on Gary Thompson's page) seem to be easily
adapted to be standard-compliant and reasonably portable.
Most of them involve ASCII coding. That's not portable.
The ones I've seen don't suppose any particular coding. The
classical example is in "http://www.acm.org/classics/sep95/",
and there's nothing that depends on ASCII there.

--
James Kanze (GABI Software) email:james.kanze:gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 4 '07 #9
On Aug 4, 1:30 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-08-04 00:48, Juha Nieminen wrote:
Victor Bazarov wrote:
Most of them involve ASCII coding. That's not portable.
How many systems can you mention which are used today and which
do not use ASCII coding?
I believe that IBM still uses EBCDIC on their z series machines.
They did the last time I looked.

(Strictly speaking, ASCII is dead, and no recent machine uses
it. But the encodings they do use, such as ISO 8859-1 or UTF-8,
do usually contain ASCII as a subset.)
Heck, *no* C++ source code is portable to systems which do not
use ASCII coding because the source code itself uses ASCII coding.
Not unless it's not written on a machine which uses ASCII, besides I
don't think the C++ standard defines how the source should be encoded,
only which characters that can be expected to be recognised.
There's a very definite reason why ftp has two modes: binary and
"ascii". Even between Windows and Unix, you need to map the
representation of '\n' (and possibly EOF).

The assumption for a self reproducing program, of course, is
that it will be compiled on the machine on which it runs, or
else that the output will be "translated" from the encoding used
on that machine to the encoding used by the machine with the
compiler. Translation which is necessary for the output of any
program generating text.

--
James Kanze (GABI Software) email:james.kanze:gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 4 '07 #10
James Kanze wrote:
: On Aug 4, 1:30 am, Erik Wikström <Erik-wikst...@telia.comwrote:
:: On 2007-08-04 00:48, Juha Nieminen wrote:
::: Victor Bazarov wrote:
:::: Most of them involve ASCII coding. That's not portable.
:
::: How many systems can you mention which are used today and which
::: do not use ASCII coding?
:
:: I believe that IBM still uses EBCDIC on their z series machines.
:
: They did the last time I looked.

They sure do!

:
::: Heck, *no* C++ source code is portable to systems which do not
::: use ASCII coding because the source code itself uses ASCII coding.
:
:: Not unless it's not written on a machine which uses ASCII, besides
:: I don't think the C++ standard defines how the source should be
:: encoded, only which characters that can be expected to be
:: recognised.
:
: There's a very definite reason why ftp has two modes: binary and
: "ascii". Even between Windows and Unix, you need to map the
: representation of '\n' (and possibly EOF).

I can add a bit of trivia to this, by explaining what happens when you
ftp between a PC and a Z-series mainframe.

The "ascii" command tells the ftp processors (one at each end!) that
the *transfer* is done in text mode. It doesn't tell anything about
how the file is encoded at either end. At the Z-series end, the ftp
processor will translate "ascii" mode transfers to/from EBCDIC, when
communicating with a non-EBCDIC device.

This means that source code actually IS portable between ASCII and
EBCDIC, if you just transfer it properly.
Bo Persson
Aug 5 '07 #11
Erik Wikström wrote:
> How many systems can you mention which are used today and which
do not use ASCII coding?

I believe that IBM still uses EBCDIC on their z series machines.
Ah, yes, I have a couple of those here.

Not.
Aug 5 '07 #12
Bo Persson wrote:
No, but you might need it to transfer your program to the mainframe.
It doesn't support floppies or USB-memories, and the operators won't
let you get even close to the CD-reader.

You could also use a terminal, and type it in again. :-)
Of course this raises the question of why you would even want to
run a self-printing program in a mainframe... :P
Aug 5 '07 #13
Btw, this is the shortest "non-portable" self-replicating valid C++
program I could come up with. It's "non-portable" because it assumes
ASCII codes are used and, most importantly, it assumes that the ASCII
value 10 is linefeed. Should work in most unix systems, though.

#include <cstdio>
int main(){char*s="#include <cstdio>%cint
main(){char*s=%c%s%c;std::printf(s,10,34,s,34);}"; std::printf(s,10,34,s,34);}
Aug 5 '07 #14
Btw, this is the shortest "non-portable" self-replicating valid C++
program I could come up with. It's "non-portable" because it assumes
ASCII codes are used and, most importantly, it assumes that the ASCII
value 10 is linefeed. Should work in most unix systems, though.

#include <cstdio>
int main(){char*s="#include <cstdio>%cint main(){char*s=%c%s%c;std::printf(s,10,34,s,34);}"; std::printf(s,10,34,s,34);}

--
- Warp
Aug 5 '07 #15
On Aug 5, 8:22 pm, Juha Nieminen <nos...@thanks.invalidwrote:

[...]
Of course this raises the question of why you would even want to
run a self-printing program in a mainframe... :P
Or any other system, for that matter. It's not as if they are
particularly useful.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 6 '07 #16

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

Similar topics

14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
1
by: Rafal Lagowski | last post by:
Hi all This is my small problem ... I have a program which make a long long statistic (about 2 minutes) It enough to timeout in my browser (or squid proxy of client) I want first make a...
1
by: Richard Hollenbeck | last post by:
I noticed I can't push a value into a text box by saying something like, "txtThisTextBox = intSomeVariable * 0.5" because I get an run-time error saying I can't assign a value to this object....
10
by: aatish19 | last post by:
1: Write a program that asks the user to input an integer value and print it in a reverse order Sample Output Enter any number 65564 Reverse of 65564 is 46556 2: Write a program that takes a...
13
by: katysei | last post by:
I have to print several large structs to print the screen. I was wondering if thers a tool to do so automaticly struct A { int i; char * s; }
9
by: cniharral | last post by:
Hi, I'm interested in printing out coloured lines of my application and I don't know what to use. Can anybody give me an idea?? Regards. Carlos Niharra López
0
by: jegathees | last post by:
Hi Friends, I am working in SAP XI Platform. In this, we use XSLT Mapping. I am facing one problem to print in field 'RecordID' starts from 1,2,3 in the target structure for those records...
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
21
by: mikhal80 | last post by:
Is it possible to write a program which would print out it's own source code, using C++?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.