@DungeonLords
In your original code you have
//Write to file
write(buf.buffer().constData(), buf_cnt);
...
//Choose A) or B)
//A) Open-close way
// buf.close();
// buf.open(QBuffer::WriteOnly);
//B) Clear way
buf.buffer().clear();
Because at start you have already written into the buf.buffer() B does not leave you in same state as A. Opening and closing in A resets the current file offset pointer to 0/beginning of file. Just calling buf.buffer().clear(); in B leaves the current file offset pointer where it is, at 2. For B you need to insert buf.seek(0L); either before or after the buf.buffer().clear(); to achieve the same situation, then it will report 1 instead of 2 just like A does.