Reputation: 191
I need to print a world file containing 6 lines of data. If I print 6 lines of one field each, nothing happens. If I print only 5 lines, data comes out correct, but the last line contains two fields of data. This does work when I remove one << endl
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;
}
this code will give no file written at all. Nothing happens.
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;
}
I tried the same with fprintf and the same problem comes up. this solution works with two fields written to the same last line:
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);
This solution gives no file written. The only difference is the \n between the last two fields:
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);
Upvotes: 0
Views: 57
Reputation: 191
I did what Quimby suggested and went through the program step by step. I found that some code actually deleted the file afterwards, but not if I removed one line feed. Bugs like that are difficult to track. So the code works well, the problem was other place. Still it is not sure why this extra line feed could delete the file.
One problem is that use of debugging in VS 2019 + CUDA, is that CUDA does not produce the images in debug mode. Things take too long time. Release mode is normally used to verify that things work. This was not a CUDA problem so debug mode showed me the problem.
The tip with use of \n instead of endl was nice. Thanks to everyone for helping me.
Upvotes: 0