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
How to Throw some light on the splay trees?
What is sizeof array in c?
The % symbol has a special use in a printf statement. How would you place this character as part of the output on the screen?
How to delete a node from linked list w/o using collectons?
What is pass by reference in c?
Difference between malloc() and calloc() function?
What are enums in c?
What is maximum size of array in c?
any restrictions have on the number of 'return' statements that may be present in a function. a) no restriction b) only 2 return statements c) only 1 return statements d) none of the above
How can I display a percentage-done indication that updates itself in place, or show one of those twirling baton progress indicators?
What is the purpose of macro in C language?
Can 'this' pointer by used in the constructor?
How can I avoid the abort, retry, fail messages?
Which built-in library function can be used to match a patter from the string?
WRITE A PROGRAM TO MERGE TWO SORTED ARRAY USING MERGE SORT TECHNIQUE..