To read data from a file fgetc() function in C is used. Reading the information from the file is major function in c programming, fgetc() function in C is used to read from file single character at a time.

What is fgetc() function in C?

fgetc(file get character)function used to read the single character at a time from file or stream.
fgetc() function in C means file get character is a file handling function in C programming that used to read single character.
For using fgetc() function in C we have to include stdio.h header file to our program.

fgetc() function in C
fgetc() function in C

fgetc() Syntax in c
char char_variable=fgetc(FILE *file_pointer);

In above syntax for fgetc function, we can see fgetc function accepts one parameter which is a file_pointer.file_pointer is a pointer to File object which holds the address of the file (location of file ) and the operation mode in which we opened file (that is read, write or append etc).
This gets function returns the character from the files which file pointer pointing that’s why we accept that character in character variable. and if file reaches to end of file or if an error occurs then fgetc returns EOF is returned.

Program to illustrate fgetc() function in c

#include<stdio.h>

#include<conio.h>

void main ()

{

FILE *fp;. // Creating File object

char ch;

printf ("\n opening file in read mode to read character from file:");

fp= fopen("technical.txt","r");. // tech.txt is file which we opening in read mode

printf("\nReading from file:");

ch= fgetc (fp); //reading first character from file

printf ("%c\n",ch);

fclose ();

}

Program description: –
In the above program, we are reading the character from a file.
Let’s assume the file technical.txt present at the location with content (content:- Source Define Loop )
First, we open the file from location in the reading mode as we reading from the file.
Now, we write fgetc function having parameter as a pointer to file object which holds the location of file and mode.
As file contains content fgetc function reads the first character from the position the file pointer is pointing and we store that into char variable.
We print that character.
Note:- This program only reads one character from a file.

Output:-

In output, we only get a first single character from the file technical.txt
fgetc() function in CIn the above program we only read the first character. now we will see reading whole file using fgetc function.

Program to read all character till the end from file

#include <stdio.h>

int main()

{

FILE *fp;

printf ("\n opening file in read mode to read character from file:");

fp = fopen("technical.txt", "r");

char ch;

printf("\nReading from file:\n");

if (fp != NULL) {

// Check whether it is the last character or not

while (!feof(fp)) {

ch = fgetc(fp);

printf("%c", ch);

fclose(fp);

}

else {

printf("\n Unable to Open the File");

}

return 0;

}

Program description: –
In the above program, we are reading all the information from the file character by character.
First, we opened the file in reading mode.
In file is exists it opened in reading mode.

Let us assume technical.txt file contains information (content:- Source Define loop: fgetc function in c)
Next, we use a while loop to iterate each and every character till end of file. As we read one character while checks, whether it reached the end. if file reached end it returns otherwise it moves the pointer to the next position.
As pointer moves to new position, we read character using fgetc function.

Output:-

fgetc() function in C

Write A Comment