-
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
-
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.
-
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
-
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
-
i figured it out, nm
thx anyways
-
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);