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 |
can anyone suggest some site name..where i can get some good data structure puzzles???
O,T,T,F,F,S,S,E,N,?,?,?,T,F,F,S,S,E,N
what is associativity explain what is the precidence for * and & , * and ++ how the folloing declaration work 1) *&p; 2) *p++;
why u join this call center?
Explain Basic concepts of C language?
How to avoid buffer overflow?
Write a program that takes a 5 digit number and calculates 2 power that number and prints it.
what is the structure?
What is structure padding and packing in c?
i want the code for printing the output as follows 4 4 3 3 2 2 1 1 0 1 1 2 2 3 3 4 4
What is the difference between declaring a variable and defining a variable?
Do you know the difference between exit() and _exit() function in c?