C Programming language

adplus-dvertising
Character I/O : How to use fgetc( ) Function in C
Previous Home Next

fgetc() Function: fgetc() is a character oriented function. this function reads the single character from a given file and increment the file pointer position. On success it return the character after converting it. It to an int without sign extension. On error it return EOF

int fgetc(FILE *stream );

If successful, it return the next requested object from the stream. Character values are returned as an unsigned char converted to an int. If the stream is at end-of-file or a read error occurs, then it return EOF.

/**  program to understand the use of fgetc( ) function */
#include <stdio.h>
void main()
{
  int c;   
/*
 * Pointer to the file. FILE is a structure  defined in <stdio.h>
 */   
FILE *ptr; 
  ptr = fopen("Ritu.txt","r");     //Open the file
/*
 * Read one character at a time, checking for the End of File.
 * EOF is defined in <stdio.h>  as -1 
 */
  while ((c = fgetc(ptr)) != EOF)
  {
    printf("%c",c);		 // O/P the character to the screen
  }
   fclose(ptr);			// Close the file.
  }
);
Previous Home Next