How are you determining that the correct number of integers is NOT being written to the file? Try inspecting the file, paying particular attention to the first number in the file.

Then, consider...