I have a c++ program that wries a simple json file using ofstream.
if( GDALGetGeoTransform( m_dataset, adfGeoTransform ) == CE_None )
{
    cout <<std::fixed << std::setprecision(3) << adfGeoTransform[0] << ", "<< adfGeoTransform[1] << ", "<< adfGeoTransform[2] << ", "<< adfGeoTransform[3] << ", "<< adfGeoTransform[4] << ", "<< adfGeoTransform[5] << ", "<<endl;
    ofstream myfile;
    myfile.open ( outPath + "/tiles.json");
    myfile << std::fixed << std::setprecision(9) ;
    myfile << "{" <<endl;
    myfile << "\"title\": \"" << filename << "\"," << endl;
    myfile << "\"ul\" : [" << dfGeoX<<", "<< dfGeoY<<"],"  << endl;     
    myfile << "\"lr\" : [" << dfGeoX1<<", "<< dfGeoY1<<"],"  << endl;
    myfile << "\"res\" : [" << adfGeoTransform[1]<<", "<< adfGeoTransform[5] <<"],"  << endl;
    if( m_dataset->GetProjectionRef()  != NULL )
    {   
        //printf( "Projection is `%s'\n", poDataset->GetProjectionRef() );
        myfile << "\"projection\" : \"";
        auto proj = m_dataset->GetProjectionRef();
        for(int i =0 ; proj[i] != '\0';i++){
            if(proj[i] == '"')
                myfile << '\\';
            myfile << proj[i];
        }
        myfile << "\"," << endl;
    }
    myfile << "\"maxZoom\" : "<< max(ceil(log2(nRows / tileSize)), ceil(log2(nCols / tileSize)))  << endl;
    myfile << "}" <<endl;
    myfile.flush();
    myfile.close();
}
I do see the console output from cout, so it is entering the if branch.
But funny thing is that when i run it the first time it do not generate tiles.json file. But running it again it creates the file.
 
    