How exactly do programs that display there own source code work?
How exactly do programs that display there own source code work?
They can work any number of ways, I've seen some that use character values to help print themselves, others that heavily use the preprocessor, and creative use of language features. This is my personal favorite because it's just so sneaky.
Code:#include <stdio.h> #include <stdlib.h> int main(void) { int c; FILE *f = fopen(__FILE__, "r"); if (f == 0) return EXIT_FAILURE; while ((c = getc(f)) != EOF) putchar(c); fclose(f); return EXIT_SUCCESS; }
My best code is written with the delete key.
>Does sneaky translate to easiest?
Yes, sneaky, isn't it?
My best code is written with the delete key.
Is it possible to display the source code of any program like a hello world program?
That program simply displays the source code of the source code that displays the source code. What if I don't want to see the source code that displays the source code?
Um...yeah.
>Is it possible to display the source code of any program like a hello world program?
Not just the hello world program, it takes a lot of work to write a program that prints itself exactly. Often the result is far from portable.
>What if I don't want to see the source code that displays the source code?
The output of the program that displays the source code is the source code, so if you don't want to see the source code of the program that displays the source code, you shouldn't write the source code of the program that displays the source code. Do something more productive than write programs that print themselves.
My best code is written with the delete key.
I'll go read the newspaper now.Originally posted by Prelude
Do something more productive than write programs that print themselves.
the way i would do it:
1)include the program and .cpp file
2)have the program infile the .cpp file
3)have a comment in the .cpp file like this:
4)write the printing algorithm to ignore these comments and everything in between them...Code:... //ignore ... // <=searching algorithm goes here //endignore ...
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
You could add your own source code as a resource right? Anyways, I wouldn't want to do that. In fact, I think displaying your own code is kinda useless. I'm guessing that you would use it as a way of Error Checking, right? In that case I would use Structured Exception Handling, and cause each function do pass some sort of identifier whenever an exception occured, that way you would be able to check out the error type, and what function it happened in... if, ofcourse, that was the reason for wanting to know how to display your own code.
"What are you after - the vague post of the week award?" - Salem
IPv6 Ready.
Travel the world, meet interesting people...kill them.
Trying to fix or change something, only guaruntees and perpetuates its existence.
I don't know about angels, but it is fear that gives men wings.
The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak.
E-Mail Xei
I just wanted to know how it's done. I didn't actually want to use it for anything. You know, like remembering a birthday. <--- That was a joke.
Though, I don't think anyone uses it for anything serious. Doing things like that is just a fun way of excercising your programming knowledge.