CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2002
    Posts
    1,798

    Serialize(CArchive& ar) ... if(ar.IsStoring) --- I don't get it !

    I have written code that saves a matrix to a utf-8 file. This code works just fine when invoked from a win32 console app, but if I attempt to use the exact same code by invoking it within a SDI Doc/View Serialize(CArchive& ar), if(ar.IsStoring), it fails, but in a somewhat unusual manner. Namely, the file shows up in the designated directory, but it is an empty file (0 bytes), and the 'Unable to open file' message is seen in Debug config. Note that both the win32 console app and the SDI app are both using a Unicode character set, however, only the SDI app uses the method inside a Serialize(CArchive& ar) method, whereas the win32 console app evokes the save method directly from within _tmain. If anyone can point out why this seemingly illogical desparity occurs I would be greatly appreciative.

    Here's the template code I have used in innumerable applications to save matrices to a disk file.

    Code:
    namespace TNT
    {
    //
    
    /// saves a TNT double matrix, discriminating between
    /// real and complex matrices by virtue of the respective file
    /// markers, 'MATCALC94R' and 'MATCALC94C'.
    template <class T>
    void SaveRealBMatrix(const Matrix<T> &M, string sfilename)
    {
    	_RPT0(0, "SaveRealBMatrix:\n");
    	_RPT1(0, "sfilename =: |%s|\n", sfilename.c_str());
    
    	long i, j, r, c;
    	r = M.num_rows();
    	c = M.num_cols();
    	ofstream f;
    	f.open(sfilename.c_str(), ios::binary | ios::out);
    	if(!f) {
    		_RPT0(0, "Unable to open file\n");  return;
    	}
    	char sFileID[12];  
    	strcpy_s(sFileID, 12, "MATCALC94R"); sFileID[11] = 0x02;
    	f.write( (char*) &sFileID, strlen(sFileID));
    	f.write( (char *) &r, sizeof(r));
    	f.write( (char *) &c, sizeof(c));
    
    	for(i=1;i<=(long)r;i++) {
    		for(j=1;j<=(long)c;j++) {
    			if ( !(f.write( (char *) &M(i, j), sizeof(double)) ) ){
    				f.close();
    				_RPT0(0, "error writing output\n");
    			}      
    		}
    	}
    	f.close();
    
    }// SaveRealBMatrix(const Matrix<T> &M, string sfilename)
    
    } // namespace TNT
    Here's the code from the SDI document Serialize(CArchive& ar) that fails as described above. Note that m_M is a class member TNT::Matrix<double> m_M that has been verified to exist in the Serialize method.

    Code:
    void CMyWndDoc::Serialize(CArchive& ar)
    {
    	if (ar.IsStoring())
    	{
    
    		CString csFileName = ar.m_strFileName;  
    		wstring ws = csFileName.GetBuffer(0);  
    		string s(ws.begin(), ws.end());
    		SaveRealBMatrix(m_M, s.c_str());
    	}
    } // namespace TNT
    [/code]
    Last edited by Mike Pliam; September 15th, 2012 at 01:06 AM.
    mpliam

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Serialize(CArchive& ar) ... if(ar.IsStoring) --- I don't get it !

    The file is already opened by CArchive class, you can only use CArchive serialization functions to write to the file. Trying to open this file you have Access denied error.

  3. #3
    Join Date
    May 2002
    Posts
    1,798

    Re: Serialize(CArchive& ar) ... if(ar.IsStoring) --- I don't get it !

    Makes sense. Thanks, Alex.
    mpliam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured