我有一个使用ofstream的简单json文件的c ++程序。
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();
}
我确实看到cout的控制台输出,所以它正在进入if分支。
但有趣的是,当我第一次运行它时,它不会生成tiles.json文件。但是再次运行它会创建文件。
答案 0 :(得分:0)
问题是它尝试创建文件的文件夹不存在。
应用程序没有因此崩溃,只是继续,然后在应用程序中,文件夹由第3方lib创建,作为生成的其他文件的输出。
然后第二次运行,文件夹存在并写入文件。