Program that prints it's own source

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khajeddin
    New Member
    • Nov 2006
    • 51

    #1

    Program that prints it's own source

    hi :i have to write a program which should print its "source code" for output.how to do this.please make me a example of "source code" and make a definition of "source code".program shouldn't contain any file .
  • Soujiro
    New Member
    • Jan 2007
    • 35

    #2
    Originally posted by khajeddin
    hi :i have to write a program which should print its "source code" for output.how to do this.please make me a example of "source code" and make a definition of "source code".program shouldn't contain any file .

    Hmmm.. its not that hard though.. you only need to use

    FILE *fp = fopen(filename, "r") "r" means read only..

    and read its byte by int read = fgetc( fp ).. use a loop

    now use printf(":)") to print the read but first cast it to (char) (ex. char m = (char)read; ).. this is so because fgetc returns an unsigned char cast to an int..
    or what do you mean by output..(where? )

    Comment

    • khajeddin
      New Member
      • Nov 2006
      • 51

      #3
      Originally posted by Soujiro
      Hmmm.. its not that hard though.. you only need to use

      FILE *fp = fopen(filename, "r") "r" means read only..

      and read its byte by int read = fgetc( fp ).. use a loop

      now use printf(":)") to print the read but first cast it to (char) (ex. char m = (char)read; ).. this is so because fgetc returns an unsigned char cast to an int..
      or what do you mean by output..(where? )
      let me describe you more:
      1) i'm not permitted to use any file in my program.
      2)i have to print my source code ( in consul)(output is source code) .

      Comment

      • Soujiro
        New Member
        • Jan 2007
        • 35

        #4
        Originally posted by khajeddin
        let me describe you more:
        1) i'm not permitted to use any file in my program.
        2)i have to print my source code ( in consul)(output is source code) .
        Hmm.. what operating system are u using.. can u use external executables.. and let those executables print the source code..

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by khajeddin
          hi :i have to write a program which should print its "source code" for output.how to do this.please make me a example of "source code" and make a definition of "source code".program shouldn't contain any file .
          Someone has already beaten you to it

          Comment

          • dasarath mood
            New Member
            • Nov 2006
            • 11

            #6
            this is the simple code:

            char*s="char*s= %c%s%c;main(){p rintf(s,34,s,34 );}";main(){pri ntf(s,34,s,34); }





            Originally posted by r035198x
            Someone has already beaten you to it

            http://www.thescripts.com/forum/thread588410.html

            Comment

            Working...