Thursday, February 4, 2010

My way of writing a Quine

Here I m presenting a Quine written by me few days before. A Quine is a program that prints its own source code. In my source code user has an option of printing the source code n number of times.

Here is the code written in c:

#include < stdio.h >
int main()
{
int n;
char c;
FILE *f;
f=fopen(__FILE__,"r");
scanf("%d",&n);
while(n--)
{
while((c=getc(f))!=EOF)
putchar(c);
fseek(f,0,0);
}
return 0;
}



Here is the code written in c++:

#include <fstream>
#include<iostream>
#include <cstdio>
using namespace std;
int main()
{
int n;
string a;
cin>>n;
while(n--)
{
ifstream fin(__FILE__);
while(!fin.eof())
{
getline(fin,a,'\n');
cout<<a><<"\n";
}
}
return 0;
}

3 comments:

  1. this is cool... thanks for the code! :)

    ReplyDelete
  2. Are you sure this is 'the quine'? I think you are just printing out the content of your code. Pardon my ignorance but I believe that ideal quine codes should not use files.

    ReplyDelete
  3. Quines are self printing codes.
    They do not read files.I have a doubt that this is a quine. And yeah plzz dont be offended

    ReplyDelete