Monday, 9 November 2015

A program to illustrate the use of fgets( )

#include <stdio.h>

int main()
{
  FILE *fp;
  char str[60];

  /* opening file for reading */
  fp = fopen("file.txt" , "r");
  if(fp == NULL) {
     perror("Error opening file");
     return(-1);
  }
  if( fgets (str, 60, fp)!=NULL ) {
     /* writing content to stdout */
     puts(str);
  }
  fclose(fp);

  return(0);
}

Output:

Demo File

No comments:

Post a Comment