fgets()

Declaration

char* fgets(char* cpBuffer, int iMaxRead, FILE* qpStream);

Description:

This function reads up to "iMaxRead" bytes into "cpBuffer" from "qpStream." The read stops when any of the following occurs; a newline character is read, the end of the steam is reached, or when (iMaxRead - 1) characters are read into the buffer. The function returns the pointer "cpBuffer" if the read is successful and NULL ( = 0), otherwise.

Example

#include <cstdio>

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

    char caBuffer[50];
    // Read in the first and only line of text
    char* cpReturn = fgets(caBuffer, 50, qpFile);
    if (cpReturn) {
        printf("Read in: %s\n", caBuffer);
    }

    fclose(qpFile);

    return 0;
}

Input File:

fgets() Input File

Output:

fgets() Output