Hello everyone, I made a code to convert .dat to .txt, and after put the .txt created in other 2 matrices (distances_1 and distances_2). The problem is that the only last element(1023,319) of the matrices are not read properly, the last element have a value of 400000 but still being show as zero. All the other elements are corrects, only the last is not read. Could Someone take a look in the code and show me my mistake?
When I get that matrix and convert it into a .txt file, and define vectors to hold the values of each element, and print std::cout << distances_1[SIZEX-1][SIZEY-1] only the last element is wrong. All the others are right, only the last is sohwing zero instead its value.
You're missing out.close() after writing the original text output file.
In your original post above it would go on line 36.
Closing the file will "flush" the buffer, writing the last few numbers out.
To certify that, I also convert distance_1 matrix into a .txt, and looking there the last element was written as zero, nevertheless all the other was wirtten correctly. The last element was correct until I convert to a matrix variable.
It can be hard to spot a little error like that. That's why it's usually best to open a file within a scope and let it automatically close at the end of the scope:
1 2 3 4 5 6 7 8 9 10 11 12
{
std::ofstream out(OutputFile);
if (!out) {
std::cout << "Error opening output file\n";
return 1;
}
for (int y = 0; y < SizeY; y++) {
for (int x = 0; x < SizeX; x++)
out << matrix[x][y] << ' ';
out << '\n';
}
} // out will automatically close here
Or, better yet, use functions and let the function scope do the work: