Q:

C Program to Display its Own Source Code as Output Using File Handling

0
In this Problem, you have to open a program ( Code ) Itself in console so likely we open any file in console so in place of any file write to program name with extension your file will be open or you can say you the same program will be open 
 
 
Explanation:- 
So basically we have to take an input from a filename as a source of program name with the extension .c/cpp/.java and many other and hit the enter after hitting enter program itself will be print on the console screen or we can say that .c/cpp/.java file contains with these extensions will be printed on screen. In other words, we can say that program itself will be open.
 

Note:- Program or File should be an extension, If you got the answer then try to solve below problem by own this is a challenge or practice that makes your programming skill better than before.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char ch, filename[25];
   FILE *fp;

   printf("Enter The File Name You Want To Open \n");
   gets(filename);

   fp = fopen(filename,"r");

   if( fp == NULL )
   {
      perror("Error While Opening The File. Or File Not Exist\n");
      exit(EXIT_FAILURE);
   }

   printf("\nYour File If Given Below %s :\n\n", filename);

   while( ( ch = fgetc(fp) ) != EOF )
   printf("%c",ch);

   fclose(fp);
   return 0;
}

 

Output:

Enter The File Name You Want To Open 

file open  itself.c

Your File IF Given Below file open itself.c:

#include<stdio.h>

#include<stdlib.h>

int main()

{

char ch filename [25];

FILE*fb;

printf("Enter The File Name You Want To Open \n");

gets(filename);

fb=fopen(filename,"r");

if(fb==NUII)

{

perror(Error While Opening The File. Or File Not Exist\n");

exit(Exit_FAILURE);

}

printf("\nYour File If Given Below %s :\n\n ",filename);

while((ch=fgetc(fb)!=EOF)

fclose(fb);

return 0;

}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now