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
Can you write the algorithm for Queue?
What is the scope of static variable in c?
I just typed in this program, and it is acting strangely. Can you see anything wrong with it?
What is volatile, register definition in C
Can include files be nested?
What are the disadvantages of a shell structure?
Why is c called c?
Why do we use & in c?
explain what is a newline escape sequence?
Is it better to use malloc() or calloc()?
What are the advantages and disadvantages of c language?
pierrot's divisor program using c or c++ code
How would you rename a function in C?
What is the value of h?
Explain data types & how many data types supported by c?