Thursday, September 11, 2014

display contents of a file on the screen using fgets function


Problem

Write a program that asks the user to input a file name. The program opens that file in read mode and displays it content on the screen. Use fgets function.

Solution - Code

#include<stdio.h>
#include<stdlib.h>
int main ()
{
 char a[10],a1[10],ch;
 FILE *fp,*ft;

 printf ("Enter file name : ");
 gets (a);

 fp = fopen (a,"r");

 if (fp == NULL)
 {
  printf ("Not found");
  exit (0);
 }

 while (1)
 {
  ch = fgetc(fp);
  if ( ch == EOF )
   break;
  printf ("%c",ch);
 }

 printf ("\n");
 system("pause");
}

Sample Output

display file content on screen

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact