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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Which operators cannot be overloaded a) Sizeof b) .* c) :: d) all of the above

654


What is a lookup table in c?

622


A character flag or control mechanism that delineates one data item from another a) variable b) constant c) delimiter d) call by reference

628


Tell me with an example the self-referential structure?

560


What do you mean by invalid pointer arithmetic?

637






Why do we use static in c?

629


Why structure is used in c?

585


what does static variable mean?

647


What is auto keyword in c?

787


why wipro wase

1823


how to write optimum code to divide a 50 digit number with a 25 digit number??

2747


How can I implement a delay, or time a users response, with sub-second resolution?

615


Can we change the value of static variable in c?

556


What is a lvalue

657


What are multidimensional arrays?

656