Fixed a bug in writing without opening a file (caused by RDWR flag)