-2

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283
  • You should use a JSON library instead of trying to format it yourself. You're likely to miss some details. – Barmar Feb 10 '18 at 21:34
  • Fun fact: A stream will automatically flush on close and automatically close when it goes out of scope. Useful reading: [What is meant by Resource Acquisition is Initialization (RAII)?](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) – user4581301 Feb 10 '18 at 21:41

1 Answers1

0

The problem was that the folder that it was trying to create the file in did not exist.

The application did not crash due to this, just continued and then later in the application the folder was created by 3th party lib as a output of other files generated.

Then running the second time, the folder exists and the file is written.

Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283
  • That's why you should check for errors when trying to open the file. – Barmar Feb 10 '18 at 21:34
  • 1
    Recommend checking the file stream for success after performing an operation on it. Streams do not throw exceptions by default, so if you do not check the stream state, you'll never see that it failed. For example, after `myfile.open ( outPath + "/tiles.json");`, an `if (myfile)` whould have told you whether or not the file had been successfully opened. A `if(myfile << "{" < – user4581301 Feb 10 '18 at 21:34