fgetc in c

If you want to know more on fgetc in c then read this blog post.

What is fgetc in c programming?

fgetc is a function which is used to obtain character input from a file.

This function takes input as character by character from specified file.

This function returns the characters read from a file as unsigned char which is casted to int.

It uses file pointer to seek the data in a file.

If the file pointer reads EOF then it terminates the process.

It is defined in stdio.h header file.

Prototype

#include<stdio.h>
int fgetc(FILE *fpointer);

Parameters of fgetc in c

fgetc takes file pointer to seek input in a file.

Return value of fgetc in c

This function returns data read as unsigned char which is casted to an int or error.


Example program for fgetc in c

Example

#include<stdio.h>
int main()
{
  FILE *fpointer;
  int n;
  fpointer=fopen("scholarsoul.txt","r");
  if(fpointer==NULL)
  {
   printf("File reading error \n");
   return(-1);
  }
  do
  {
   n=fgetc(fpointer);
   if(n==EOF)
    break;
   else 
    printf("%c \n",n);
   }while(1);
  fclose(fpointer);
  return 0;
}
Example for fgetc in c
Example for fgetc in c

Code explanation

The input is taken from a file by calling fgetc function. The content of the file is printed onto the console character by character.

fgetc Man page


Difference between fgetc and fseek in c

fgetc is a function that is used to obtain input from a file. fseek is used to change the file pointer to the specified stream.

fgetc returns the characters read from a file. The fseek function returns zero in operation is successful otherwise a nonzero value.

fgetc takes one argument. fseek takes three arguments.

fgetc moves pointer from the beginning of a file. fseek moves the pointer depending upon whence argument value specified.

Prototypes are as follows

int fgetc(FILE *fpointer);

int fseek(FILE *strm,long int offst,int whnce);
fgetc in cfseek in c
Used to obtain input from a file.Used to change the file pointer to the specified stream.
int fgetc(FILE *fpointer);int fseek(FILE *strm,long int offst,int whnce);
Returns the characters read from a file.Returns zero in operation is successful otherwise a nonzero value.

Difference between fgetc and fputc in c

fgetc is a function which is used to obtain input from a file. fputc is used to write single character at a time to a file.

fgetc returns the characters read from a file. fputc returns character written in a file otherwise EOF as error.

fgetc takes one argument. fputc takes two arguments.

Prototypes are as follows

int fgetc(FILE *fpointer);

int fputc(int char,FILE *fpointer);
fgetc in cfputc in c
Used to obtain input from a file.Used to write single character at a time to a file.
int fgetc(FILE *fpointer);int fputc(int char,FILE *fpointer);
Returns the characters read from a file.Returns character written in a file otherwise EOF as error.

Difference between fgetc and putc in c

fgetc is a function which is used to obtain input from a file. putc function writes a character to the specified stream.

fgetc returns the characters read from a file. putc returns the character written as an unsigned char cast to an int or error.

fgetc takes one argument. putc takes two arguments.

Prototypes are as follows

int fgetc(FILE *fpointer);

int putc(int char,FILE *strm);
fgetc in cputc in c
Used to obtain input from a file.Writes a character to the specified stream.
int fgetc(FILE *fpointer);int putc(int char,FILE *strm);
Returns the characters read from a file.Returns the character written as an unsigned char cast to an int or error.

Difference between fgetc and fgets in c

fgetc is a function which is used to obtain input from a file. fgets reads a line from a specified stream.

fgetc returns the characters read from a file. fgets returns the string read from a file or null pointer if error occurs.

fgetc takes one argument. fgets takes three arguments.

Prototypes are as follows

int fgetc(FILE *fpointer);

char *fgets(char *string,int m,FILE *strm);
fgetc in cfgets in c
Used to obtain input from a file.Reads a line from a specified stream.
int fgetc(FILE *fpointer);char *fgets(char *string,int m,FILE *strm);
Returns the characters read from a file.Returns the string read from a file or null pointer if error occurs.

Difference between fgetc and fputs in c

fgetc is a function which is used to obtain input from a file. fputs writes a string to the specified stream.

fgetc returns the characters read from a file. fputs returns a non-negative value or EOF on error.

fgetc takes one argument. fputs takes two arguments.

Prototypes are as follows

int fgetc(FILE *fpointer);

int fputs(const char *string,FILE *stream);
fgetc in cfputs in c
Used to obtain input from a file.Writes a string to the specified stream.
int fgetc(FILE *fpointer);int fputs(const char *string,FILE *stream);
Returns the characters read from a file.Returns a non-negative value or EOF on error.

People also ask for

What is the difference between fread and fgetc in c?

fgetc reads one character at a time. Whereas fread reads some number of records at a time.

What is the difference between getc and fgetc in c?

fgetc cannot be implemented as macro. But getc can be implemented as macro.

What does fgetc stands for?

fgetc stands for get character from stream. It is defined in stdio.h header file.

What is the difference between fscanf and fgetc in c?

fgetc reads input from a file. fscanf reads formatted input data from a file.