Results 1 to 6 of 6

Thread: GetFile

  1. #1

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471

    GetFile

    I'm using the following code in my Serialize function to store
    and retrieve pure text to/from a text file into an edit control.

    How would I get the text of that control and read/write to the
    CFile?

    Code:
    void CMyProgDoc::Serialize(CArchive& ar)
    {
                   const CFile* fp = ar.GetFile();
    
    	if (ar.IsStoring())
    	{
    	       // What do i do here to write the control into
    	       // the file?
    	}
    	else
    	{
    	       // What do i do here to write the file into
    	       // the control?
    	}
    }
    thx for any help
    Bababooey
    Tatatoothy
    Mamamonkey

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Since you are using the document/view architecture, you should seperate the storing of the text (CMyProgDoc) from the displaying (CMyProgView). I assume that your edit control is a child of your view window. The view window should regularly (best is always when edit control looses focus) copy the contents of the edit control to a CString object in the document. In the document you could simply write:
    Code:
    void CMyProgDoc::Serialize(CArchive& ar)
    {
                   const CFile* fp = ar.GetFile();
    	if (ar.IsStoring())
    	{
    	       // Assuming your string is called m_strContent
    	       ar << m_strContent;
    	}
    	else
    	{
    	       ar >> m_strContent;
    	}
    	// maybe this happens automatically
    	UpdateAllViews(NULL);
    }
    In CMyProgView::OnUpdate you get the string from the document and write it to the edit control.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    CornedBee,
    I did what you recommended and my program is giving me an
    'unexpected file format' error when I attempt to open one of my
    files. If my file is empty however, i don't get the error. It's when
    the text file has content that I get this error. Any idea?

    Thank you
    Bababooey
    Tatatoothy
    Mamamonkey

  4. #4

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    i debugged this problem and it attempts to

    ar >> m_strEditorText;

    but the debugger runs to the following code within the
    DOCCORE.CPP file

    Code:
    CATCH_ALL(e)
    	{
    		ReleaseFile(pFile, TRUE);
    		DeleteContents();   // remove failed contents
    
    		TRY
    		{
    			ReportSaveLoadException(lpszPathName, e,
    				FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
    		}
    		END_TRY
    		DELETE_EXCEPTION(e);
    		return FALSE;
    	}
    	END_CATCH_ALL
    Bababooey
    Tatatoothy
    Mamamonkey

  5. #5

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    i figured it out, nm
    thx anyways
    Bababooey
    Tatatoothy
    Mamamonkey

  6. #6

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    CornedBee,

    I got the contents of the file to be read into the CString object
    then into my control. I have two problems however....

    1) when reading the contents of the file to the CString then the
    control, some garbage characters are inserted to the end.....i'm
    assuming these are the \0 characters. how do i fix this?

    2) how would i get this operation to work the other way around?
    i.e. save the contents of the control to a cstring object then
    saving the CString object to a text file?

    here is my code so far

    Code:
    CFile* pFile = ar.GetFile();
    	
    
    if (ar.IsStoring())
    {	
    
    }
    else
    {	
    	const UINT nLen = pFile->GetLength();
    	char* pBuf = new char[nLen];
    		
    	pFile->Read(pBuf, nLen);
    		
    	CString strTemp(pBuf);
    	m_strEditorText = strTemp;
    
    	delete pBuf;
    }
    
    UpdateAllViews(NULL);
    Bababooey
    Tatatoothy
    Mamamonkey

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