flush() bug?
- Posted by Andy Serpa <ac at onehorseshy.com> Sep 22, 2005
- 448 views
Using the following program with exw.exe: ----------------------------- include file.e include get.e integer fn fn = open("test.txt","w") puts(fn,"Hello") close(fn) ? gets(0) ------------------------------------------- The file "test.txt" is created and the console hangs until you hit enter. So far so good. The file test.txt should be "unlocked" (because we close()'d it) and it is -- I can move it, rename, etc with the console still open (so the program hasn't terminated.) If we change the middle section to: ------------- fn = open("test.txt","w") puts(fn,"Hello") flush(fn) close(fn) ------------- It also works the same way, as expected. However, if we do this: -------------- fn = open("test.txt","w") puts(fn,"Hello") flush(fn) puts(fn,"Hello") close(fn) -------------- The file test.txt now stays locked even though it has been closed -- it won't release until we terminate the program. In other words, if we use flush() at any point, and then write data to the file after the flush() and then close(), the file stays locked. The fix: -------------- fn = open("test.txt","w") puts(fn,"Hello") flush(fn) puts(fn,"Hello") flush(fn) close(fn) -------------- We have to add an extra flush() before the close() to get it to release. An easy fix, but still the behavior is not correct unless I'm missing something...