Fixed appending to end of file to work with iOS

Idea and testing by PavelVolkovitskij
Fixed EOF reference
Fixed ranges checking to ignore remaining empty ranges