Q:

How to use fgetc in C Programming

belongs to collection: File handling in C

0

The fgetc() function read a single character from the stream and return their ASCII value. After reading the character, it advances the associated file position indicator for the stream. It takes only one argument file stream.

In this article, I will explain to you, how to read the character from the given file using fgetc in C programming with example. The fgetc function obtains that character as an unsigned char converted to an int.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In the below code, I am reading a file using the fgetc. The file “aticleworld.txt” contains a string “I love File handling.”.

#include <stdio.h>
 
int main()
{
    //file pointer
    int ch = 0;
    FILE *fp = NULL;
 
    //open the file in read
    fp = fopen("aticleworld.txt", "r");
    if(fp == NULL)
    {
        printf("Error in creating the file\n");
        exit(1);
    }
 
    while( (ch=fgetc(fp)) != EOF )
    {
        //Display read character
        printf("%c", ch);
    }
 
    //close the file
    fclose(fp);
 
    printf("\n\n\nRead file successfully\n");
 
    return 0;
}

Output:

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< How to use fputc in C programming...