How to make a program that prints it's own source code???

This is a discussion on How to make a program that prints it's own source code??? within the C++ Programming forums, part of the General Programming Boards category; But will you please tell me how will the source code be build??if we are making the source code for ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,959
    But will you please tell me how will the source code be build??if we are making the source code for the 1st time.That means during compilation there's no .obj file on hard disk!
    This is just the job of the compiler.

    the object file that doesn't exist at all..can we assign it's address to some file pointer?
    No. You are heading in the wrong direction. To begin with, the object code is different from the source code.

    It will do if it's not following the rules. I just want to do the job of printing the source code..
    Then you either write a true quine that prints its own source code without any file I/O at all, or you simply provide a copy of the source file along with the program, and read from that file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chottachatri View Post
    And i know the source code is compiled into object code. But will you please tell me how will the object code be build??if we are making the source code for the 1st time.That means during compilation there's no .obj file on hard disk!i hope you getting my point??the object file that doesn't exist at all..can we assign it's address to some file pointer??
    I presume you are discussing the objective of producing a program that prints itself, and the "chicken and egg problem" caused by that? Having source code print itself is pretty pointless in itself - it's only useful in "academic" circles. And somehow, the source file MUST know it's own content to be able to print it. It's a bit like asking a painter paint himself doing a self-portrait of himself - he must then first paint a self-portrait, right [although, technically, this can be painted onto the same canvas as the painting of himself painting said-self portrait. But just like this example, if you want the source code to print itself, it first needs to have the source code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    And i know the source code is compiled into object code. But will you please tell me how will the object code be build??
    A compiler takes C/C++ code as input, and outputs object code that can be run on the CPU directly. Object code is basically a string of very low level CPU instructions. It looks nothing like and does not include the original source code. The most you can get if you try really hard is probably printing out an assembly version of your program, by reading the memory directly, and disassemble the opcode. I am not sure if even THAT is possible.

  4. #19
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    if we are making the source code for the 1st time.That means during compilation there's no .obj file on hard disk
    A compiler converts source code into object code. It's not a one-to-one relationship, however. Therefore, you cannot convert the object code back to the source code (actually there are programs that convert object code into C/C++, but it's probably not what you want, because the resulting source code will look nothing like your original code, just something equivalent in functionality).

    Let the compiler be a function "C(s) = obj" where s is your source code, and obj is the resulting object code. The function is many-to-one, therefore, there exists no inverse function that converts obj back to your starting s.

  5. #20
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by vart View Post
    The question is not so stupid. Everyone with the "classical" programming education is aware of such program. So if the person learns about it during interview it can indicate possible "shortcuts" in his education curve... And this can be futher investigated with other questions
    But this has no useful purpose in a real-world application. Interviewers should be more concerned with asking questions that are relevent to the job.

  6. #21
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by cpjust View Post
    But this has no useful purpose in a real-world application. Interviewers should be more concerned with asking questions that are relevent to the job.
    The "Classical" education from some good University is useful in a real-world application. But instead of asking question what courses you have accomplished- they can ask questions that will show what you have actually learned...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #22
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by vart View Post
    The "Classical" education from some good University is useful in a real-world application. But instead of asking question what courses you have accomplished- they can ask questions that will show what you have actually learned...
    They can see whether or not you have a "classical" education by looking at your resume. They should be asking you intelligent questions about situations that you will encounter on the job and which help validate your work experience.
    I'd say that actual knowledge and experience are worth a lot more than fancy degrees that just prove you paid a lot of money for a piece of paper.

  8. #23
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    They can see whether or not you have a "classical" education by looking at your resume
    I could write in my resume whatever I want. Interviewer should make sure - all that is written in the resume is actually true, or close to the trueth...

    I can write in the resume I have studied assembler in the University. Which it true. But it was a small course take more than 15 years ago... Since then I never programed in asm... Do you think I could answer any question about assembler now?

    So the resume is just a starting point for the interview.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #24
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by vart View Post
    I could write in my resume whatever I want. Interviewer should make sure - all that is written in the resume is actually true, or close to the trueth...

    I can write in the resume I have studied assembler in the University. Which it true. But it was a small course take more than 15 years ago... Since then I never programed in asm... Do you think I could answer any question about assembler now?

    So the resume is just a starting point for the interview.
    Yeah, that's exactly what I mean. They ask should questions to verify a person's skills. If they really want to discriminate against people without a certain type of education, they can just ask to see your degree or contact the school to verify...

  10. #25
    Registered User
    Join Date
    Nov 2002
    Posts
    303
    why dont you use like
    get the window handle of the compiler , or ide or what ever your executable is called
    then use SendMessage WM_GETTEXT , and read every line in to a buffer

    Handle HandleofCompiler;
    FindWindow(NULL,"your compiler name"):
    char buffer[32];
    if this returns ok , then you have a handle

    now you can use SendMessage(HandleofCompiler , WM_GETTEXT , 0 , LPARAM(buffer));

    altho i am not sure how you would move to the next line ,,,
    Last edited by Anddos; 03-27-2008 at 04:52 AM.

  11. #26
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Anddos View Post
    why dont you use like
    get the window handle of the compiler , or ide or what ever your executable is called
    then use SendMessage WM_GETTEXT , and read every line in to a buffer

    Handle HandleofCompiler;
    FindWindow(NULL,"your compiler name"):
    char buffer[32];
    if this returns ok , then you have a handle

    now you can use SendMessage(HandleofCompiler , WM_GETTEXT , 0 , LPARAM(buffer));

    altho i am not sure how you would move to the next line ,,,
    Because that would only work if you know which IDE you're using, assuming you're using one at all, and it would only work when you run the program from the IDE, which you'd only do when writing it.

  12. #27
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Besides the obvious solution, read the source file and print it.
    The other and easy too, solution is write the program first. Then declare a string variable containing the
    same text as your program. Now your source is changed to a kind of mirror image. The same source text exists twice. One in original form, and other in string form. Now just print that string twice, by whatever means. A little trick may be needed. But thats it.

  13. #28
    Registered User
    Join Date
    Nov 2002
    Posts
    303
    work when you run the program from the IDE, which you'd only do when writing it.

    not true , you could run the program exe from any directory , not just from the run menu of the compiler ,
    all you would have todo is have the compiler open showing the source code ,

    do you mean you want to view the source code to output even with the compiler/ide closed?

    maybe make some kind of decrytor ,

    like if the varible is a int in your program , then use sizeof() to confirm it
    then output int , but wont be able to get the varible name
    Last edited by Anddos; 03-27-2008 at 07:12 AM.

  14. #29
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Anddos View Post
    work when you run the program from the IDE, which you'd only do when writing it.

    not true , you could run the program exe from any directory , not just from the run menu of the compiler ,
    all you would have todo is have the compiler open showing the source code ,

    do you mean you want to view the source code to output even with the compiler/ide closed?

    maybe make some kind of decrytor ,

    like if the varible is a int in your program , then use sizeof() to confirm it
    then output int , but wont be able to get the varible name
    or even better -- don't waste your time with nonsense like this. It has absolutely no purpose and your time would be better spent doing just about anything else.

  15. #30
    Registered User
    Join Date
    Nov 2002
    Posts
    303
    cpjust what kind of reply is that , i am only trying to help , keep your comments to your self

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Replies: 1
    Last Post: 03-12-2002, 05:31 AM
  4. C source code for int25 or code help
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-26-2001, 02:04 AM

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