Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


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

Answers were Sorted based on User's Feedback



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

Answer / yogesh bansal

#include <stdio.h>

int main()
{
FILE *file;
char filename[]="outputsourcecode.c";
char str[125];
file=fopen(filename,"r");
if(file == NULL)
{
printf("cannot open the file");
exit(1);
}
while(!feof(file))
{
if(fgets(str,125,file))
{
printf("%s", str);
}
}
return 0;
}

this is a working code.

Is This Answer Correct ?    4 Yes 0 No

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

Answer / satish

using FILE concept we can achieve this

main()
{
FILE *fp;
FILE *fp1;
char ch;
int c=0;
clrscr();
fp=fopen("c:\cc.txt","r");
fp1=fopen("abc.txt","w");
if(fp==NULL)
{
printf("\n source file opening error");
//exit(1);
}
else if(fp1==NULL)
{
printf("\n target file opening error");
//exit(1);
}
while(!feof(fp))
{
ch=fgetc(fp);
fputc(ch,fp1);
c++;
}
fclose(fp1);
fclose(fp);
fp1=fopen("abc.txt","r");
while(!feof(fp1))
{
ch=fgetc(fp1);
//fprintf(fp1,"%c",ch);

printf("%c",ch);
}
fclose(fp1);
printf("\n %d bytes copied",c);
c=fcloseall();
printf("\n%d files closed",c);
getch();
}

Is This Answer Correct ?    4 Yes 2 No

Post New Answer

More C Interview Questions

What are loops c?

0 Answers  


write a program to count the no of repaeted words in a line?

1 Answers  


C program to perform stack operation using singly linked list

3 Answers  


What is void main ()?

0 Answers  


What is the right way to use errno?

0 Answers  






What are the Advantages of using macro

0 Answers  


How would you print out the data in a binary tree, level by level, starting at the top?

6 Answers   Amazon, Microsoft,


explain what is an endless loop?

0 Answers  


a=(1,2,3); b=1,2,3; c=1,(2,3); d=(1,2),3; what's the value of 'a','b','c','d'

2 Answers  


What are .h files and what should I put in them?

3 Answers  


for(;;) printf("C language") What is out put of above??

2 Answers   Practical Viva Questions,


Why pointers are used in c?

0 Answers  






Categories