October 15, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1583

           Summary: std.cstream.CFile cannot be detached from FILE*
           Product: D
           Version: unspecified
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: wbaxter@gmail.com


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.


-- 

October 19, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1583


thomas-dloop@kuehne.cn changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         OS/Version|Windows                     |All




------- Comment #1 from thomas-dloop@kuehne.cn  2007-10-19 12:23 -------
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));
# }


--