File.open("foo", "w") do |file|
file.write "foo"
end
because everyone always says it's _dangerous_ to leave files open...
just, wondering, what could really happen?
suppose i write a program that write to a file, infinitely, i'll want to
shut this program off after a while, the file will not properly close...
what could actually happen?
suppose i write a program that write to a file, infinitely, i'll want to
shut this program off after a while, the file will not properly close...
what could actually happen?
You can lost data
moulon% ls -l xxx
ls: xxx: No such file or directory
moulon%
With buffering IO your changes might not be written to the file (this
might depend on OS and the way your process terminates). Also, if you
open the same file multiple times from the same process you might also
see weird effects. Lastly, keeping a file descriptor open for longer
than needed may cause you troubles if you open many files, because
tehre is a limit on the max number of open file descriptors.
Note that in your scenario you can alternatively do the closing in
at_exit or END block depending on how you architect your software. If
the sole task of the software is to write to a single file then I'd
probably still rather use the block approach and have your main
program inside that block.
File.open("foo", "w") do |file|
file.write "foo"
end
because everyone always says it's _dangerous_ to leave files open...
just, wondering, what could really happen?
suppose i write a program that write to a file, infinitely, i'll want to
shut this program off after a while, the file will not properly close...
what could actually happen?
No expert here, and I'm sure it depends on the OS. But I think the biggest danger you face is loosing some of the data you wrote to the file. It's possible that some data is in a buffer and hasn't actually been written when the program exits. Closing a file writes the buffer, so not closing it and just exiting may leave buffer data unwritten.
-Mat
···
On Jun 6, 2006, at 9:45 AM, Dirk Meijer wrote:
hello list!
i've learnt to open files like this:
File.open("foo", "w") do |file|
file.write "foo"
end
because everyone always says it's _dangerous_ to leave files open...
just, wondering, what could really happen?
suppose i write a program that write to a file, infinitely, i'll want to
shut this program off after a while, the file will not properly close...
what could actually happen?
The major issue with not closing files on *Nix is that you *could* run out of file descriptors for that process. The OS will close the file at program termination, but you can and should use hooks like at_exit{} and END {} to make sure (If you need to keep one file open indefinitely). STDOUT for instance is a file, you don't see most people closing that.
···
On Jun 6, 2006, at 9:45 AM, Dirk Meijer wrote:
hello list!
i've learnt to open files like this:
File.open("foo", "w") do |file|
file.write "foo"
end
because everyone always says it's _dangerous_ to leave files open...
just, wondering, what could really happen?
suppose i write a program that write to a file, infinitely, i'll want to
shut this program off after a while, the file will not properly close...
what could actually happen?
All files should be closed on exit, assuming exit!() wasn't called.
James Edward Gray II
···
On Jun 6, 2006, at 8:53 AM, Mat Schaffer wrote:
But I think the biggest danger you face is loosing some of the data you wrote to the file. It's possible that some data is in a buffer and hasn't actually been written when the program exits. Closing a file writes the buffer, so not closing it and just exiting may leave buffer data unwritten.
> But I think the biggest danger you face is loosing some of the data
> you wrote to the file. It's possible that some data is in a buffer
> and hasn't actually been written when the program exits. Closing a
> file writes the buffer, so not closing it and just exiting may
> leave buffer data unwritten.
FWIW, kill -KILL and program crashes loses written but unsynced data.
···
On 6/6/06, James Edward Gray II <james@grayproductions.net> wrote:
On Jun 6, 2006, at 8:53 AM, Mat Schaffer wrote:
All files should be closed on exit, assuming exit!() wasn't called.