Reputation: 132
std::ofstream myfile;
myfile.open ("adsf.txt");
int g [800];
for (int i = 0; i < 800; i++)
{
for (int j = 0; j < 129; j++)
{
if((int)img.getPixel(i,j).a>111){
myfile<<(j+353);
myfile<<"\n";
g[i]=j+353;
break;
}
}
}
myfile.close();
This code results in a text file with these values, from lines 364-384:
481 481 481 481 481 481 481 481 481 481 481 481 481 481 481 481 481 481 481 481 481
These values are correct.
However, if I use a for loop to check the array after it's made, it results in these (and other, all of them in the middle of the file) being corrupted:
for (int h = 0; h < 800; h++)
{
myfile<<g[h];
myfile<<"\n";
}
myfile.close();
results in
481 481 481 481 481 481 481 481 481 481 481 2004341180 1994199293 344 0 1994199326 1314101291 14019080 14019376 15809736 36
This corruption continues until line 489, when the file becomes accurate again.
Can anyone tell me what I'm doing wrong?
Upvotes: 1
Views: 121
Reputation: 52471
I bet you end up with fewer than 800 numbers in your file.
When the inner loop doesn't find a j
for which the condition is satisfied, it a) doesn't output anything to the file, and b) leaves g[i]
uninitialized, containing random garbage.
Upvotes: 3