Index: /trunk/Mars/mcore/ofits.h
===================================================================
--- /trunk/Mars/mcore/ofits.h	(revision 17750)
+++ /trunk/Mars/mcore/ofits.h	(revision 17751)
@@ -42,6 +42,9 @@
 // units: m kg s rad sr K A mol cd Hz J W V N Pa C Ohm S F Wb T Hlm lx
 
-class ofits : public std::ofstream
+class ofits : public std::ostream
 {
+protected:
+    std::filebuf fFilebuf;
+
 public:
     struct Key
@@ -225,5 +228,5 @@
         Checksum checksum;
 
-        void Out(std::ofstream &fout)
+        void Out(std::ostream &fout)
         {
             if (!changed)
@@ -359,17 +362,48 @@
 
 public:
-    ofits() : fCommentTrimming(false),
-              fManualExtName(false)
-    {
-    }
-    ofits(const char *fname) : std::ofstream(),
-                               fCommentTrimming(false),
-                               fManualExtName(false)
-    {
-        this->open(fname);
-    }
-    virtual ~ofits() { if (is_open()) close(); }
-
-    virtual void open(const char * filename, bool addEXTNAMEKey=true)
+    ofits()
+        : std::ostream(), fFilebuf(), fCommentTrimming(false), fManualExtName(false)
+    {
+        init(&fFilebuf);
+    }
+
+    ofits(const char *fname)
+        : std::ostream(), fFilebuf(), fCommentTrimming(false), fManualExtName(false)
+    {
+        init(&fFilebuf);
+        open(fname);
+    }
+
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+    ofits(const std::string &fname)
+        : std::ostream(), fCommentTrimming(false), fManualExtName(false)
+    {
+        init(&fFilebuf);
+        open(fname);
+    }
+#endif
+
+    virtual ~ofits()
+    {
+        if (is_open())
+            close();
+    }
+/*
+    filebuf *rdbuf() const
+    {
+        return const_cast<filebuf*>(&fFilebuf);
+    }
+*/
+    bool is_open()
+    {
+        return fFilebuf.is_open();
+    }
+
+    bool is_open() const
+    {
+        return fFilebuf.is_open();
+    }
+
+    virtual void open(const char *filename, bool addEXTNAMEKey=true)
     {
         fDataSum  = 0;
@@ -380,11 +414,11 @@
 
         SetStr("XTENSION", "BINTABLE", "binary table extension");
-        SetInt("BITPIX",                     8, "8-bit bytes");
-        SetInt("NAXIS",                      2, "2-dimensional binary table");
-        SetInt("NAXIS1",                     0, "width of table in bytes");
-        SetInt("NAXIS2",                     0, "number of rows in table");
-        SetInt("PCOUNT",                     0, "size of special data area");
-        SetInt("GCOUNT",                     1, "one data group (required keyword)");
-        SetInt("TFIELDS",                    0, "number of fields in each row");
+        SetInt("BITPIX",  8, "8-bit bytes");
+        SetInt("NAXIS",   2, "2-dimensional binary table");
+        SetInt("NAXIS1",  0, "width of table in bytes");
+        SetInt("NAXIS2",  0, "number of rows in table");
+        SetInt("PCOUNT",  0, "size of special data area");
+        SetInt("GCOUNT",  1, "one data group (required keyword)");
+        SetInt("TFIELDS", 0, "number of fields in each row");
         if (addEXTNAMEKey)
             SetStr("EXTNAME", "", "name of extension table");
@@ -394,5 +428,13 @@
         SetStr("DATASUM",  "         0", "Checksum for the data block");
 
-        std::ofstream::open(filename);
+        if (!fFilebuf.open(filename, ios_base::out|ios_base::trunc))
+            setstate(ios_base::failbit);
+        else
+            clear();
+    }
+
+    virtual void open(const std::string &filename, bool addEXTNAMEKey=true)
+    {
+        open(filename.c_str(), addEXTNAMEKey);
     }
 
@@ -740,5 +782,5 @@
 
 
-    Checksum WriteHeader(std::ofstream &fout)
+    Checksum WriteHeader(std::ostream &fout)
     {
         Checksum sum;
@@ -930,5 +972,4 @@
     Checksum UpdateHeaderChecksum()
     {
-
         std::ostringstream dataSumStr;
         dataSumStr << fDataSum.val();
@@ -951,10 +992,10 @@
 
         // We don't have to jump back to the end of the file
-        SetInt("NAXIS2",  fTable.num_rows);
-
+        SetInt("NAXIS2", fTable.num_rows);
 
         const Checksum chk = UpdateHeaderChecksum();
 
-        std::ofstream::close();
+        if (!fFilebuf.close())
+            setstate(ios_base::failbit);
 
         if ((chk+fDataSum).valid())
Index: /trunk/Mars/mcore/zofits.h
===================================================================
--- /trunk/Mars/mcore/zofits.h	(revision 17750)
+++ /trunk/Mars/mcore/zofits.h	(revision 17751)
@@ -106,8 +106,21 @@
                uint32_t numTiles    = DefaultMaxNumTiles(),
                uint32_t rowPerTile  = DefaultNumRowsPerTile(),
-               uint32_t maxUsableMem= DefaultMaxMemory()) : ofits(fname),
-                   fMemPool(0, maxUsableMem*1000),
-                   fWriteToDiskQueue(std::bind(&zofits::WriteBufferToDisk, this, std::placeholders::_1), false)
-        {
+               uint32_t maxUsableMem= DefaultMaxMemory()) : ofits(),
+            fMemPool(0, maxUsableMem*1000),
+            fWriteToDiskQueue(std::bind(&zofits::WriteBufferToDisk, this, std::placeholders::_1), false)
+        {
+            open(fname);
+            InitMemberVariables(numTiles, rowPerTile, maxUsableMem*1000);
+            SetNumThreads(DefaultNumThreads());
+        }
+
+        zofits(const std::string &fname,
+               uint32_t numTiles    = DefaultMaxNumTiles(),
+               uint32_t rowPerTile  = DefaultNumRowsPerTile(),
+               uint32_t maxUsableMem= DefaultMaxMemory()) : ofits(),
+            fMemPool(0, maxUsableMem*1000),
+            fWriteToDiskQueue(std::bind(&zofits::WriteBufferToDisk, this, std::placeholders::_1), false)
+        {
+            open(fname);
             InitMemberVariables(numTiles, rowPerTile, maxUsableMem*1000);
             SetNumThreads(DefaultNumThreads());
@@ -199,4 +212,9 @@
             fCatalogSum.reset();
             fRawSum.reset();
+        }
+
+        void open(const std::string &filename, bool addEXTNAMEKey=true)
+        {
+            open(filename.c_str(), addEXTNAMEKey);
         }
 
@@ -513,5 +531,6 @@
             const Checksum checksm = UpdateHeaderChecksum();
 
-            std::ofstream::close();
+            if (!fFilebuf.close())
+                setstate(ios_base::failbit);
 
             fSmartBuffer = std::shared_ptr<char>();
