Bug 1583 – std.cstream.CFile cannot be detached from FILE*

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2007-10-15T02:04:00Z
Last change time
2015-11-03T17:43:07Z
Assigned to
nobody
Creator
wbaxter

Comments

Comment #0 by wbaxter — 2007-10-15T02:04:34Z
The CFile class provides a nice wrapper around a C FILE*, but it provides NO way of disconnecting itself from the file, and ultimately will try to close whatever file you pass to it. In particular I think the behavior of the file(FILE*) setter and the constructor should be considered buggy. They pretend the FILE is open even if it is NULL. If those methods actually checked for null, and didn't set the isopen flag in those cases, then setting to null could be used as a way to detach the FILE. As is, if you set the FILE* to null, there's no crash, but the CFile will inevitably try to call fclose() on that null handle, which it should not, since that will cause errno to get set. Anyway, there should be a way to detach the file from the CFile. I suggest just using "set to null" to signal that, but I'd be happy with anything.
Comment #1 by thomas-dloop — 2007-10-19T12:23:14Z
I use the following CFileEternal class. Compile with/without -version=kludge to see the difference. # import std.stream : FileMode; # import std.cstream : CFile; # import std.c.stdio : FILE, fopen, ftell; # import std.string : toStringz; # import std.stdio : writefln; # # class CFileEternal : CFile{ # this(FILE* cfile, FileMode mode, bool seekable = false) { # super(cfile, mode, seekable); # } # # ~this(){ # flush(); # isopen = readable = writeable = seekable = false; # } # } # # void main(char[][] args){ # auto name = args[1]; # FILE* f = fopen(toStringz(name), "w"); # # if(!f){ # throw new Exception("failed to open " ~ name); # } # # { # version(kludge){ # scope cf = new CFileEternal(f, FileMode.Out); # }else{ # scope cf = new CFile(f, FileMode.Out); # } # } # # writefln("FILE \"%s\" has been closed: %s", name, -1 == ftell(f)); # }
Comment #2 by andrei — 2015-11-03T17:43:07Z
It's unlikely this D1 issue will get worked on, if anyone plans to work on it feel free to reopen.