Function fgetc () in C

The function int fgetc (FILE * stream) in C Library standard takes the next character (an unsigned char) from the given Stream and increases the position indicator for that Stream.

The function int fgetc (FILE * stream) in C Library standard takes the next character (an unsigned char) from the given Stream and increases the position indicator for that Stream.

Declare function fgetc () in C

Below is the declaration for fgetc () in C:

 int  fgetc ( FILE  * stream ) 

Parameters

stream - This is the pointer to a FILE object that identifies Stream on which the operation is performed.

Returns the value

This function returns the character read as an unsigned char that is cast to an int or EOF or an error.

For example

The following program C illustrates the usage of fgetc () in C:

 #include int  main  () {  FILE  * fp ; int  c ; int  n  = 0 ;  fp  =  fopen ( "baitapc.txt" , "r" ); if ( fp  ==  NULL ) {  perror ( "Xay ra loi khi mo baitapc.txt !!!" ); return (- 1 ); } do {  c  =  fgetc ( fp ); if (  feof ( fp ) ) { break ; }  printf ( "%c" ,  c ); } while ( 1 );  fclose ( fp ); return ( 0 ); } 

Suppose we have baitapc.txt with the following content. This file will be used as input for C program for example:

Picture 1 of Function fgetc () in C

Compiling and running the above C program will result:

Picture 2 of Function fgetc () in C

According to Tutorialspoint

Previous lesson: The function sscanf () in C

Next lesson: Function fgets () in C

You've just finished reading the article "Function fgetc () in C" edited by the TipsMake team. You can save function-fgetc-in-c.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.

« PREV : Function fgets () in...
The function sscanf... : NEXT »