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
Which operators cannot be overloaded a) Sizeof b) .* c) :: d) all of the above
What is a lookup table in c?
A character flag or control mechanism that delineates one data item from another a) variable b) constant c) delimiter d) call by reference
Tell me with an example the self-referential structure?
What do you mean by invalid pointer arithmetic?
Why do we use static in c?
Why structure is used in c?
what does static variable mean?
What is auto keyword in c?
why wipro wase
how to write optimum code to divide a 50 digit number with a 25 digit number??
How can I implement a delay, or time a users response, with sub-second resolution?
Can we change the value of static variable in c?
What is a lvalue
What are multidimensional arrays?