Monday, 16 May 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

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment