If you're still wedded to your notion (I strongly urge you to use the proper way with temp file and renaming, even making it a function if you need to repeat this several times) :

readModifyWrite fileName modify = do
  contents <- readFile fileName
  evaluate (length contents) -- ensure that contents is evaluated and the handle closed
  writeFile fileName (modify contents)

This should work (but may consume a fair bit of memory, you should probably use Text or ByteString anyway and use the proper way instead).

--
Jedaï