int fscanf(FILE* qpStream, const char* kcpFormatString, ...);


This function reads the formatted string "kcpFormatString" from the stream given by the "qpStream" argument. For information on string formatting, see the scanf page. If successful, the function returns the number of fields that were successfully read and assigned. If an error occurs or the end of the stream is reached before the first assignment, the function returns EOF.


#include <cstdio>

int main()
    char* cpFileName = "XoaX.txt";
    // Open an ASCII text file for reading.
    FILE* qpFile = fopen(cpFileName, "r" );

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

    char cBuffer[50];
    // Read in the first and only string of the file.
    int iRet = fscanf(qpFile, "%s", cBuffer);
    if (iRet != EOF) {
        printf("Read in: %s\n", cBuffer);


    return 0;

Input File:

fscanf() Input File


fscanf() Output