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


This function writes the formatted string "kcpFormatString" to the stream given by the "qpStream" argument. For information on string formatting, see the printf page. If successful, the function returns the number of bytes written. If an error occurs, the function returns a negative number.


#include <cstdio>

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

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

    // Append the copyright to the file,
    // with a decimal number and string
    char caBuf[] = "Xoax";
    int iReturn = fprintf(qpFile, "\nCopyright %d %s", 2009, caBuf);

    if (iReturn < 0) {
        printf("Write Error");


    return 0;

Input File:

fprintf() Input

Output File:

fprintf() Output File


fprintf() Output