ofstream与fprintf写入6行world文件失败,合并行则正常
问题描述
我需要生成一个包含6行数据的world文件。使用ofstream时,若每行写入单个字段共6行,文件无任何输出;若将最后两个字段合并到同一行(共5行),则数据输出正常。使用fprintf时也出现相同问题:最后两个字段用空格分隔在同一行可成功写入,用换行分隔成两行则无文件输出。相关代码示例如下:
可正常输出的ofstream代码
double ResEast = 0.05; double ResNorth = -0.05; double UTMMinEast = 459947.750; double UTMMaxNorth = 7564825.450; std::ofstream outputfile; outputfile.open(worldoutname, std::ofstream::out); outputfile << std::setprecision(12); if (outputfile.is_open()) { outputfile << ResEast << endl; outputfile << "0.0" << endl; outputfile << "0.0" << endl; outputfile << ResNorth << endl; outputfile << UTMMinEast << " " ; outputfile << UTMMaxNorth << endl; outputfile.close(); } else { std::cerr << "didn't write" << std::endl; }
无输出的ofstream代码
double ResEast = 0.05; double ResNorth = -0.05; double UTMMinEast = 459947.750; double UTMMaxNorth = 7564825.450; std::ofstream outputfile; outputfile.open(worldoutname, std::ofstream::out); outputfile << std::setprecision(12); if (outputfile.is_open()) { outputfile << ResEast << endl; outputfile << "0.0" << endl; outputfile << "0.0" << endl; outputfile << ResNorth << endl; outputfile << UTMMinEast << endl; outputfile << UTMMaxNorth << endl; outputfile.close(); } else { std::cerr << "didn't write" << std::endl; }
可正常输出的fprintf代码
FILE* pFile; const int length = worldoutname.length(); // declaring character array (+1 for null terminator) char* char_array = new char[length + 1]; // copying the contents of the // string to char array strcpy(char_array, worldoutname.c_str()); fopen_s(&pFile,char_array,"w"); double ResEast = 0.05; double ResNorth = -0.05; double UTMMinEast = 459947.750; double UTMMaxNorth = 7564825.450; fprintf_s(pFile, "%f\n0.0\n0.0\n%f\n%f %f\n", ResEast, ResNorth, UTMMinEast, UTMMaxNorth); fclose(pFile);
无输出的fprintf代码
FILE* pFile; const int length = worldoutname.length(); // declaring character array (+1 for null terminator) char* char_array = new char[length + 1]; // copying the contents of the // string to char array strcpy(char_array, worldoutname.c_str()); fopen_s(&pFile,char_array,"w"); double ResEast = 0.05; double ResNorth = -0.05; double UTMMinEast = 459947.750; double UTMMaxNorth = 7564825.450; fprintf_s(pFile, "%f\n0.0\n0.0\n%f\n%f\n%f\n", ResEast, ResNorth, UTMMinEast, UTMMaxNorth); fclose(pFile);
内容的提问来源于stack exchange,提问作者User1953
相关产品推荐
相关产品推荐

