Quines

This is a discussion on Quines within the C++ Programming forums, part of the General Programming Boards category; How exactly do programs that display there own source code work?...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Quines

    How exactly do programs that display there own source code work?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Does sneaky translate to easiest?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Does sneaky translate to easiest?
    Yes, sneaky, isn't it?
    My best code is written with the delete key.

  5. #5
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    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.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

  7. #7
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by Prelude
    Do something more productive than write programs that print themselves.
    I'll go read the newspaper now.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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:
    Code:
    ...
    //ignore
    ... // <=searching algorithm goes here
    //endignore
    ...
    4)write the printing algorithm to ignore these comments and everything in between them...
    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

  9. #9
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    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

  10. #10
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21