locked
Is it possible to write a program which when executed prints itself as the output RRS feed

  • Question

  • Hello Sir/Madam,

    Is it possible to write a program which when executed prints itself as the output?

    For Example:

    If my program is

    public sub PrintMethodItself()

    {

    // some coding to print....

    }

    my expected output is:

    public sub PrintMethodItself()

    {

    // some coding to print....

    }

    • Changed type Nandhini.M Friday, December 21, 2012 8:52 AM
    • Moved by Naomi N Sunday, December 23, 2012 7:03 AM Better answer can be here (From:Transact-SQL)
    Friday, December 21, 2012 8:51 AM

Answers

All replies

  • Wrong forum. Try here http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/threads

    everything is a matter of probability...

    Friday, December 21, 2012 8:54 AM
  • I don't think that's possible ...

    Regards
    Satheesh

    Friday, December 21, 2012 9:43 AM
  • Post your prob. on http://www.asp.net/ forum


    Ahsan Kabir Please remember to click Mark as Answer and Vote as Helpful on posts that help you. This can be beneficial to other community members reading the thread. http://www.aktechforum.blogspot.com/

    Friday, December 21, 2012 9:48 AM
  • Yes, it is possible and about 10+ years ago I wrote such program in C++ I think it's possible in other languages as well, but I would start from C# or C++.

    In the meantime, I am moving your thread to C# forum.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    Sunday, December 23, 2012 7:01 AM
  • Hello,

    Yes it's possible, you can do that through reflection but that would make the program pretty slow, to get the expected output you will have to build a parser to build the AST and and finally a formatter that uses it to produce the output.

    Today it's easier to do it thanks to the Mono - Cecil project and Roslyn.

    Why do you need to do it?


    Regards,

    Eyal Shilony


    • Edited by Eyal Solnik Sunday, December 23, 2012 8:12 AM
    • Marked as answer by Lisa Zhu Monday, December 31, 2012 5:37 AM
    Sunday, December 23, 2012 7:47 AM
  • Hello,

     

    I remember reading an article regarding a topic similar if not identical to this issue. It was based on the program itself containing a copy of itself in hex in an (character) array - writing a preprocessor to take the file and generate a string of hex bytes and then printing those bytes twice in different ways.

    Hopefully this is useful.

    Best of Luck,

    Happy coding. :)


    Sunday, December 23, 2012 9:02 AM
  • The program I wrote for this in C++ (C actually) was rather simple and contained printf statements as I remember. The only trick was to use chr function for the " character.

    I think we also wrote similar program in Visual Foxpro.



    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    Sunday, December 23, 2012 3:58 PM
    • Edited by Naomi N Sunday, December 23, 2012 4:09 PM
    Sunday, December 23, 2012 4:08 PM
  • Hi Nandhini.M,

    I provisionally marked Eyal's & Naomi's reply as answer.

    Please  feel  free to unmark it if you think the information does not help.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 31, 2012 5:38 AM