1. close(writefile)
- Posted by useless_ Dec 20, 2012
- 1182 views
So i have writefile open, and i am writing, and the debugger says writefile equals 4, and now i want to close it. So i close(writefile). The debugger says writefile is still 4. I cannot test the variable writefile in any way for being open or closed or an error. At best, i can write code to make it a sequence and subsequently test for it's lack of atom-ness.
Can we have close(writefile) change the handle writefile to zero, please?
useless
2. Re: close(writefile)
- Posted by DerekParnell (admin) Dec 20, 2012
- 1206 views
So i have writefile open, and i am writing, and the debugger says writefile equals 4, and now i want to close it. So i close(writefile). The debugger says writefile is still 4. I cannot test the variable writefile in any way for being open or closed or an error. At best, i can write code to make it a sequence and subsequently test for it's lack of atom-ness.
Can we have close(writefile) change the handle writefile to zero, please?
No. This is due to a technical issue in that a routine cannot permanently change the value held in any argument passed to it. The scope of a parameter is limited to the routine, so any changes the routine makes to a parameter will not remain after the routine returns.
If you are using version 4 or above, as a work around, you could create your own close function, for example ...
function close(integer handle) -- Avoid closing STDIN, STDOUT, STDERR, and already closed files. if handle > 2 then eu:close(handle) -- Call the built-in close function return -1 end if return handle end function integer fh fh = open("someFile") ? fh --> 3 or some other value fh = close(fh) ? fh --> -1
It might be possible to change the built-in close routine into a function that returns -1 when it successfully closes a file, but we'd have to think that through a bit more for ramifications to existing code.
3. Re: close(writefile)
- Posted by useless_ Dec 20, 2012
- 1203 views
It might be possible to change the built-in close routine into a function that returns -1 when it successfully closes a file, but we'd have to think that through a bit more for ramifications to existing code.
But -1 is an error flag. A file being closed isn't an error, i have at least 100 lbs of closed files. For now, i set writefile to zero to detect if i ever opened the writefile in question. So 4 = i'm writing, -1 = there's some error, 0 = better open it first. Come to think of it, maybe some other (nested) flag(s) which also show(s) how it was open, like "ab", or "w", and when, and maybe who.
useless
4. Re: close(writefile)
- Posted by jimcbrown (admin) Dec 20, 2012
- 1205 views
But -1 is an error flag. A file being closed isn't an error
That's a good point. However, 0 is a valid file handle and normally represents stdin.
In the nix world, you can actually close stdin and later reuse the zeroth file handle when opening up a new file (for reading or writing, or both).
Maybe it should return -2, or 0.1 ?
5. Re: close(writefile)
- Posted by jimcbrown (admin) Dec 20, 2012
- 1163 views
It might be possible to change the built-in close routine into a function that returns -1 when it successfully closes a file, but we'd have to think that through a bit more for ramifications to existing code.
Another good idea (probably in addition to this, but even on its own) would be to add an is_open() function, that takes an integer and test whether or not that integer currently can be used to represent a valid, open file.
6. Re: close(writefile)
- Posted by useless_ Dec 20, 2012
- 1200 views
But -1 is an error flag. A file being closed isn't an error
That's a good point. However, 0 is a valid file handle and normally represents stdin.
In the nix world, you can actually close stdin and later reuse the zeroth file handle when opening up a new file (for reading or writing, or both).
Maybe it should return -2, or 0.1 ?
Can you write to stdin? If variable_name "writefile" is set to stdin, it's still not an open write_file, and i must open() (assign a valid write file handle) to it. The code i'd write to test it being zero isn't broken by nix saying it's pointed to stdin, it still tells me it's not an open writing file.
useless
7. Re: close(writefile)
- Posted by jimcbrown (admin) Dec 20, 2012
- 1209 views
Can you write to stdin?
No, but under the right conditions you can write to a file even if the file handle is 0.
If variable_name "writefile" is set to stdin, it's still not an open write_file,
How can you tell apart the case where 0 is pointing to stdin (or another file only opened for reading) versus the case where 0 is pointing to a file opened for writing?
The code i'd write to test it being zero isn't broken by nix saying it's pointed to stdin, it still tells me it's not an open writing file.
Normally, it'd be true that the zeroth file handle is not available for writing.
In some unusual cases however, the code you'd write to test it being zero would be broken because 0 is a valid file handle to write to.
Still, your test is probably good enough for what you are trying to do, and I'm the last person to argue against a 95% solution.
8. Re: close(writefile)
- Posted by DerekParnell (admin) Dec 20, 2012
- 1167 views
It might be possible to change the built-in close routine into a function that returns -1 when it successfully closes a file, but we'd have to think that through a bit more for ramifications to existing code.
But -1 is an error flag. A file being closed isn't an error, i have at least 100 lbs of closed files. For now, i set writefile to zero to detect if i ever opened the writefile in question. So 4 = i'm writing, -1 = there's some error, 0 = better open it first. Come to think of it, maybe some other (nested) flag(s) which also show(s) how it was open, like "ab", or "w", and when, and maybe who.
Actually, -1 is -1. Whether you wish your application to interpret it as a error code or not is at your discretion. I just used -1 to indicate a non-valid file handle value. I could have used a different value but it doesn't matter which at this point in the discussion, so long as it can never be a valid file handle.
We probably should have a function that takes a file handle and returns the current 'state' of the handle. Eg ... closed, open for read/write/append, etc ... and maybe a few other things about it.
9. Re: close(writefile)
- Posted by useless_ Dec 21, 2012
- 1192 views
Ok, i re-read the manual, and open() can return a zero as an open file, which sounds wierd. Ok, i'll set it to something else when i close it, like "closed".
useless
10. Re: close(writefile)
- Posted by mattlewis (admin) Dec 21, 2012
- 1138 views
So i have writefile open, and i am writing, and the debugger says writefile equals 4, and now i want to close it. So i close(writefile). The debugger says writefile is still 4. I cannot test the variable writefile in any way for being open or closed or an error. At best, i can write code to make it a sequence and subsequently test for it's lack of atom-ness.
Can we have close(writefile) change the handle writefile to zero, please?
I don't think that's a good solution. It might be better to add some functions to be able to query the back end as to the status of a particular file handle.
In the mean time, you could write some simple wrappers around open and close that keep track of which file handles are being used.
Matt
11. Re: close(writefile)
- Posted by petelomax Dec 22, 2012
- 1059 views
Another good idea (probably in addition to this, but even on its own) would be to add an is_open() function, that takes an integer and test whether or not that integer currently can be used to represent a valid, open file.
There is a problem with this.
integer fn = open(this) close(fn) integer fn2 = open(that)
Both fn and fn2 will have the same value (3)
Pete
12. Re: close(writefile)
- Posted by jimcbrown (admin) Dec 22, 2012
- 1121 views
Another good idea (probably in addition to this, but even on its own) would be to add an is_open() function, that takes an integer and test whether or not that integer currently can be used to represent a valid, open file.
There is a problem with this.
integer fn = open(this) close(fn) integer fn2 = open(that)
Both fn and fn2 will have the same value (3)
Pete
Both fn and fn2 can be used for reading and/or writing to, so I don't see a problem with that - both have the same integer and represent the same open file.
The only obvious thing I see here would be mistaken identity - one assumes fn points to this even though fn really is pointing to that. (This is not unique to Euphoria though but applies to any program in any language that uses file descriptors (fds).)
I suppose it wouldn't be too much trouble to add another function that can attempt to return a filename for a given file descriptor (when applicable of course), but at this point you might be better off not using file handles at all, instead using read_file() and write_file() or read_lines() and write_lines(), and simply always passing the filename in as a string.
13. Re: close(writefile)
- Posted by useless_ Dec 22, 2012
- 1117 views
I suppose it wouldn't be too much trouble to add another function that can attempt to return a filename for a given file descriptor (when applicable of course), but at this point you might be better off not using file handles at all, instead using read_file() and write_file() or read_lines() and write_lines(), and simply always passing the filename in as a string.
Or a hash of the filename is the integer handle.
I'd prefer the file descriptor be bidrectional on all the data about the file. It's why i usually maintain variables: readfilename, readfile, writefilename, writefile, readfileptr, etc etc.. Sometimes these are nested sequences: readfilenames = {"name1","name2","name3"}, readfiles = {handle1,handle2,handle3} etc.
Constructive critique done, i'd feel a need to bite someone's hand and point out that file handles are recycled and never run out, but task id are not recycled and do run out. Here's some ointment for that bite.
useless
14. Re: close(writefile)
- Posted by jimcbrown (admin) Dec 22, 2012
- 1135 views
Or a hash of the filename is the integer handle.
I feel this would be too much trouble - what happens if two different filenames end up with the same hash? At the very least, I'd want to see a working implementation.
I'd prefer the file descriptor be bidrectional on all the data about the file. It's why i usually maintain variables: readfilename, readfile, writefilename, writefile, readfileptr, etc etc.. Sometimes these are nested sequences: readfilenames = {"name1","name2","name3"}, readfiles = {handle1,handle2,handle3} etc.
I'm inclined to agree with you here.
Constructive critique done, i'd feel a need to bite someone's hand and point out that file handles are recycled and never run out, but task id are not recycled and do run out. Here's some ointment for that bite.
I confess that I'm not a fan of Euphoria tasks. When I need something like it, I prefer to use fork().
Again, I'm inclined to agree with you here (that task ids probably should be recycled, unless someone can think of a compelling reason why they shouldn't be).
15. Re: close(writefile)
- Posted by useless_ Dec 22, 2012
- 1082 views
Or a hash of the filename is the integer handle.
I feel this would be too much trouble - what happens if two different filenames end up with the same hash? At the very least, I'd want to see a working implementation.
If they hash to the same id and that id has been used before in the same program, add a lil something to the id from the random number generator until it's not the same any more. Some people are fans of hashes, i am not. It's an idea. In what i am doing, where an app may run thru millions of files, i don't know how where the pitfalls in hashes will be, or how to not fall into them. That said, i think if i recall a hashed handle, praps Eu can open that same file again. It may be a solution searching for a problem. It's not something i will write code for myself, i'll just keep doing what i am currently doing, because it works for me. Your mileage may vary.
useless
16. Re: close(writefile)
- Posted by jimcbrown (admin) Dec 22, 2012
- 1085 views
Or a hash of the filename is the integer handle.
I feel this would be too much trouble - what happens if two different filenames end up with the same hash? At the very least, I'd want to see a working implementation.
If they hash to the same id and that id has been used before in the same program, add a lil something to the id from the random number generator until it's not the same any more.
That seems even more problematic. Again, I'd want to see a working implementation.
That said, i think if i recall a hashed handle, praps Eu can open that same file again. It may be a solution searching for a problem.
I see no benefit to this as opposed to just passing the filename as a string everytime.
Some people are fans of hashes, i am not. It's an idea. In what i am doing, where an app may run thru millions of files, i don't know how where the pitfalls in hashes will be, or how to not fall into them. It's not something i will write code for myself, i'll just keep doing what i am currently doing, because it works for me. Your mileage may vary.
Fair enough. You were just brainstorming, but you don't necessarily advocate or support this idea. Maybe someone else will pick up on it and like it, and write the implementation for it.
Until then, I'm with you - not a fan of hashes (at least not for this particular usage of hashes anyways).