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

What is static and volatile in c?

0 Answers  


What is 02d in c?

0 Answers  


What is memmove?

1 Answers   Oracle,


what is the c source code for the below output? 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1

0 Answers  


i want to asked a question about c program the question is: create a c program that displays all prime numbers less than 500? using looping statement

5 Answers  






Are the expressions * ptr ++ and ++ * ptr same?

0 Answers  


write a program to display the frequency of each element in a given array in c language

1 Answers  


application of static variables in real time

1 Answers   Bosch,


hello everybody can we change a the adress of a variabl i mean can i put for exemple for a int *p: &p=6 ?????????

1 Answers  


How to find the digits truncation when assigning the interger variable to the character variables. like int i=500; char x = i : here we have truncation. how to find this. another ex: i =100; char x=i. here we do not have truncation.

1 Answers   HCL,


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

0 Answers   MGM,


Differentiate between the expression “++a” and “a++”?

0 Answers  


Categories