How do you write a program which produces its own source
code as its output?
Answers were Sorted based on User's Feedback
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 |
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 |
What is static and volatile in c?
What is 02d in c?
What is memmove?
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
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
Are the expressions * ptr ++ and ++ * ptr same?
write a program to display the frequency of each element in a given array in c language
application of static variables in real time
hello everybody can we change a the adress of a variabl i mean can i put for exemple for a int *p: &p=6 ?????????
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.
how to write optimum code to divide a 50 digit number with a 25 digit number??
Differentiate between the expression “++a” and “a++”?