5/16/2016

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

Problem:- C Program to Display its own Source Code as its Output or C Program to Display its own Source Code as Output or write a c program which produces its own source code as its output or Printing source code of a C program itself or C Program to Display its own Source Code as its Output or write a c program which produces its own source code as its output or Can anyone help me write a C++ program to display its own source.


Question:- 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.

Check- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard Domain.


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.


Solution:-

#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:-


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


You May Like This

1. C++ Program For Reading A Number From File And Sum Of No. Line By Line Using File

2. C++ Program To Merge Two File Into Third File Using File Handling

3. C++ Program For Copy One File To Another Using File Handling

4. C++ Program To Display The List Of Current Directory/Folder Using File

5. C++ Program For Count A Character In File Using File

6. Hacker Rank Solution For Birthday Cake Candles

7. C++ Program For Store Employee Information And Display Using Structure

8. C Program For Find A Grade Of Given Marks Using Switch Case

9. C Program For HEAP Sort In Ascending Order Also Show Complexity

10. C++ Program For Selection Sort Using Function

No comments:

Post a Comment