I have following code which should print array of 3072 integers:
for(int q=0; q < 3072; q++) printf("%x", band->GetData(q));
one would assume that it will print 3072 integers, however I get 3075 integers with 3 probably garbage ones at the end. Printing to the file using this code
fp=fopen("filename", "w");
fwrite(band->GetBuffer(), sizeof(int), 3072, fp);
fclose(fp);
ends almost the same, except there are 3 extra bytes at the end. Use of different arrays gives different length of garbage.
I would like to ask why that happens and whether it is really a garbage.
Thank you.
int GetData(unsigned int pos) const { ASSERT(pos < m_size); return m_data[pos]; }
int* GetBuffer() { return m_data; }
The problem with the
fwriteis probably because you’ve opened the file in text mode, and any0x0abytes written to the file will be expanded to0x0d 0x0a.The problem with the
printfis that you’re not putting out any separators between the numbers, so you’re miscounting the output.