Ah wait.... I think I understand. First, you need to change your algorithm. Why? your algorithm calculates the new ascii code for a given ascii code. But you should consider that ascii characters are not all text and characters, but also include control codes. Check them out at
www.asciitable.com
If the algorithm convers a certain value to the End of File value, and it is written to the file, then when the file is opened again, only characters till the end of file is read, which is not till the real end of file, but the EOF value written by the algorithm.
So change your algo to avoid those control chars and you should be okay. I had a similar problem with my class 12 projects, then found this after looking at the file with Hex Workshop