setbuf()

Declaration

void setbuf(FILE* qpStream, char* cpBuffer);

Description:

This function sets the buffer for the stream "qpStream" to the passed in buffer "cpBuffer." The function must refer to a stream that has been opened, but that has not been read or written. Also, the buffer pointer "cpBuffer," must point to a buffer of length BUFSIZ bytes. Note that this function has been deprecated in favor of setvbuf.

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 the file %s\n",  cpFileName);
        return 1;
    }
    // The buffer size must be BUFSIZ
    char caBuffer[BUFSIZ];
    // Set the buffer for the file stream
    setbuf(qpFile, caBuffer);

    // Read from the file.
    char caRead[50];
    fscanf(qpFile, "%s", caRead);

    printf("String read: %s\n", caRead);

    fclose(qpFile);
    return 0;
}

Input File:

setbuf() Input File

Output:

setbuf() Output