in reply to how can I combine into one method

After looking over the two functions a couple of times, I can't see how they differ! Could you please highlight the differences?

One thing I do note is that the return values of the various system interface functions used (open, close, etc.) are never checked: this is almost always a bad idea.