How do you write a program which produces its own source code as its output?

Showing Answers 1 - 4 of 4 Answers

bappa

  • Jun 7th, 2006
 

write this program and save it as pro.c....run....it will show the program to the consol and write the source code into data.txt file...................

#include<stdio.h>
void main()
{
 FILE *fp,*ft;
 char buff[100];
 fp=fopen("pro.c","r");
 if(fp==NULL)
 {
  printf("ERROR");
 }
 ft=fopen("data.txt","w+");
 if(ft==NULL)
 {
  printf("ERROR");
 }
 while(getc(fp)!=EOF)
 {
  
  fscanf(fp,"%s",buff);
  printf("%s\n",buff);
  fprintf(ft,"%s\n",buff);
 }
}

Biju Nair

  • Jun 29th, 2006
 

A program that produces its complete source code as its only output is called a quine.

Eg:

char*f="char*f=%c%s%c;main()
printf(f,34,f,34,10);%c";
main()printf(f,34,f,34,10);

Jayashree

  • Jul 26th, 2006
 

#include "stdafx.h"#include "string.h"#include "process.h"int main(int argc, char* argv[]){ char cmd[125]; strcpy(cmd, "type "); strcat(cmd, __FILE__); system(cmd); return 0;}

  Was this answer useful?  Yes

Ashish

  • Jan 7th, 2007
 

include<iostream>
#include<fstream>
using namespace std;
int main()
{
             fstream *obj = new fstream();
             obj->open ("main.cpp"); // file path
              cout<<obj->rdbuf();
              return 0;
}

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions