C Standard Libraries C++

fgetc()

Declaration

int fgetc(FILE* qpStream);

Description

Reads in a single character from "qpStream" and returns its value. If the read was successful, the character that was read in is returned. Otherwise, the function returns EOF to indicate an error.

Input File

fgetc() Input File

Example

#include <cstdio>

int main()
{
    char* cpFileName = "XoaX.txt";
    FILE* qpFile = fopen(cpFileName, "r" );

    // Check that the file could be opened
    if (!qpFile) {
        printf("Could not open %s for reading \n",  cpFileName);
        return 1;
    }

    // Read the first byte of the file.
    int iFirstChar = fgetc(qpFile);
    if (iFirstChar != EOF) {
        // Output it as a char.
        printf("Byte Read: %c\n", iFirstChar);
    } else {
        printf("Read Error");
    }

    fclose(qpFile);

    return 0;
}

Output

fget() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.