C Standard Libraries C++

fread()

Declaration

size_t fread(void* vpBuffer, size_t qSize, size_t qCount, FILE* qpStream);

Description

This function reads data from "qpStream" into the buffer, "vpBuffer." The argument "qSize" specifies the size, in bytes, of each item that is read and "qCount" specifies the number of items. The function returns that number of bytes that were actually read. This should be qSize*qCount, but it may be less if an error occurs or the reading reaches the end of the file.

Input File

fread() Input File

Example

#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 caBuffer[10];
    // Read in the first four characters of the file.
    size_t qReturn = fread(caBuffer, 1, 4, qpFile);
    // Add a null terminator.
    caBuffer[qReturn] = '';
    if (qReturn > 0) {
        printf("Read in: %s\n", caBuffer);
    }

    fclose(qpFile);

    return 0;
}

Output

fread() Output
 

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