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

How to write a multi-statement macro?

0 Answers  


What is output redirection?

0 Answers  


write a c program to remove all the duplicate characters in a string and replace with single character? ex:-input- AAABBBCCC output- ABC

2 Answers   HCL,


Hai friends im a i year student. i want to develop my knowledge in the field of TSR in c. How I'm Improve ?

2 Answers  


main() { printf("hello%d",print("QUARK test?")); }

5 Answers  






in one file global variable int i; is declared as static. In another file it is extern int i=100; Is this valid ?

2 Answers   NetApp,


exit () is used to a) exit () terminates the execution of the program itself b) exit () terminates the execution of the loop c) exit () terminates the execution of the block d) none of the above

0 Answers  


What is size of union in c?

0 Answers  


What are the advantage of c language?

0 Answers  


will u please send me the placement papers to my mail???????????????????

0 Answers  


n=7623 { temp=n/10; result=temp*10+ result; n=n/10 }

7 Answers   Wipro,


Why pointers are used in c?

0 Answers  


Categories