Monday, 16 May 2016

C Program For Open A File (Or Open A Same Program Itself ) Itself Using File Handling

Problem :- Write A C Program For Open A File (Or Open A Program Itself ) Itself Using File Handling .

Logic :- In this Problem you have to open a program ( Code ) Itself in console so like we open a 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 same program will be open 

Note :- Program or File should be 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 then before .

Try Yourself C++ Program To Merge Two File Into Third File Using File Handling

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 For Open A File (Or Open A Same Program Itself ) Itself Using File Handling


No comments:
Write comments

Recommended Posts × +