Latest web development tutorials

C library functions - fgetc ()

C standard library - <stdio.h> C standard library - <stdio.h>

description

C library functionsint fgetc (FILE * stream) obtained from the specified stream stream next character (an unsigned character), and the location identifier move forward.

statement

Here is () statement fgetc function.

int fgetc(FILE *stream)

parameter

  • stream - This is a pointer to FILE pointer to an object, the object identifier FILE flow to perform operations on it.

return value

This function as an unsigned char cast to an int return the character read, or if the end of file read error occurs, it returns EOF.

Examples

The following example demonstrates the fgetc () function is used.

#include <stdio.h>

int main ()
{
   FILE *fp;
   int c;
   int n = 0;
  
   fp = fopen("file.txt","r");
   if(fp == NULL) 
   {
      perror("打开文件时发生错误");
      return(-1);
   }
   do
   {
      c = fgetc(fp);
      if( feof(fp) )
      {
          break ;
      }
      printf("%c", c);
   }while(1);

   fclose(fp);
   return(0);
}

Suppose we have a text filefile.txt, it reads as follows.As an example of the file, enter:

We are in 2014

Let's compile and run the above program, which will result in the following:

We are in 2014

C standard library - <stdio.h> C standard library - <stdio.h>