C Standard Libraries C++

freopen()

Declaration

FILE* freopen(const char* kcpPath, const char* kcpMode, FILE* qpStream);

Description

This function reassigns the reading or writing of "qpStream" to the newly opened file given by "kcpPath." The argument "kcpMode" specifies the mode for opening the new file and the newly opened stream is returned via the FILE pointer. The function returns NULL if the call fails.

Input File

freopen() Input File

Example

#include <cstdio>

int main()
{
    char* cpFileName = "XoaX.txt";
    // Open an ASCII text file for reading and
    // redirect stdin to it.
    FILE* qpFile = freopen(cpFileName, "r", stdin);

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

    // Use stdin to read the first char of the file
    int iReturn = getchar();
    if (iReturn != EOF) {
        printf("Read in: %c\n", (char)iReturn);
    }
    fclose(qpFile);

    return 0;
}

Output

freopen() Output
 

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