Dumb CFile question..
-
Hello, This is a dumb question but I'm using the following to stream RTF from a .rtf file to a CRichEditCtrl.. The problem is that it's only reading in the 4096, I understand it's because my cBuf is only 4096.. Is there a way to read the whole file in with out having to declare the size of the cBuf char?? Heres my code..
// READ THE .RTF FILE IN.
CFile fileRead;
if(fileRead.Open(strFile,CFile::modeRead))
{
char cBuf[4096];
UINT uBytesRead;while(uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1)) { cBuf\[uBytesRead\] = NULL; SetRTF(CString(cBuf)); } fileRead.Close();
}
Whoever said nothing's impossible never tried slamming a revolving door!
-
Hello, This is a dumb question but I'm using the following to stream RTF from a .rtf file to a CRichEditCtrl.. The problem is that it's only reading in the 4096, I understand it's because my cBuf is only 4096.. Is there a way to read the whole file in with out having to declare the size of the cBuf char?? Heres my code..
// READ THE .RTF FILE IN.
CFile fileRead;
if(fileRead.Open(strFile,CFile::modeRead))
{
char cBuf[4096];
UINT uBytesRead;while(uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1)) { cBuf\[uBytesRead\] = NULL; SetRTF(CString(cBuf)); } fileRead.Close();
}
Whoever said nothing's impossible never tried slamming a revolving door!
You can use CRichEditCtrl::ReplaceSel to append text in to the control. Pavel Sonork 100.15206
-
You can use CRichEditCtrl::ReplaceSel to append text in to the control. Pavel Sonork 100.15206
It's not stopping at 4096.. but it is stopping before the end.. I wonder if the CRichEditCtrl has a text/size limit (I didn't declare one in my dialogs Init). I changed the 4096 to 1024 and it still reads in the same amount.. I re-wrote the code like this..
// READ THE .RTF FILE IN.
CFile fileRead;
if(fileRead.Open(strFile,CFile::modeRead))
{
char cBuf[1024];
UINT uBytesRead;
CString strBuff;while(uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1)) { cBuf\[uBytesRead\] = NULL; strBuff += CString(cBuf); } SetRTF(strBuff); fileRead.Close();
}
Whoever said nothing's impossible never tried slamming a revolving door!
-
It's not stopping at 4096.. but it is stopping before the end.. I wonder if the CRichEditCtrl has a text/size limit (I didn't declare one in my dialogs Init). I changed the 4096 to 1024 and it still reads in the same amount.. I re-wrote the code like this..
// READ THE .RTF FILE IN.
CFile fileRead;
if(fileRead.Open(strFile,CFile::modeRead))
{
char cBuf[1024];
UINT uBytesRead;
CString strBuff;while(uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1)) { cBuf\[uBytesRead\] = NULL; strBuff += CString(cBuf); } SetRTF(strBuff); fileRead.Close();
}
Whoever said nothing's impossible never tried slamming a revolving door!
I think it is because your while ( condition ) is testing the result of an assignment operation instead of testing for an end of file condition. Try something like this
uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1);
while ( uBytesRead == 1024 )
{
strBuff += CString(cBuf);
uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1));
}strBuff += CString(cBuf);
SetRTF(strBuff);
fileRead.Close();Chris Meech "what makes CP different is the people and sense of community, things people will only discover if they join up and join in." Christian Graus Nov 14, 2002. "AAAAAAAAAHHHHHH!!!!! Those leaks are driving me crazy! How does one finds a memory leak in a garbage collected environment ??! Daniel Turini Nov. 2, 2002.
-
It's not stopping at 4096.. but it is stopping before the end.. I wonder if the CRichEditCtrl has a text/size limit (I didn't declare one in my dialogs Init). I changed the 4096 to 1024 and it still reads in the same amount.. I re-wrote the code like this..
// READ THE .RTF FILE IN.
CFile fileRead;
if(fileRead.Open(strFile,CFile::modeRead))
{
char cBuf[1024];
UINT uBytesRead;
CString strBuff;while(uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1)) { cBuf\[uBytesRead\] = NULL; strBuff += CString(cBuf); } SetRTF(strBuff); fileRead.Close();
}
Whoever said nothing's impossible never tried slamming a revolving door!
I think, You need to read the whole file and then set it, because of the RTF format which is structured, and can't be added into RichEdit in chunks split at random. Pavel Sonork 100.15206
-
I think it is because your while ( condition ) is testing the result of an assignment operation instead of testing for an end of file condition. Try something like this
uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1);
while ( uBytesRead == 1024 )
{
strBuff += CString(cBuf);
uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1));
}strBuff += CString(cBuf);
SetRTF(strBuff);
fileRead.Close();Chris Meech "what makes CP different is the people and sense of community, things people will only discover if they join up and join in." Christian Graus Nov 14, 2002. "AAAAAAAAAHHHHHH!!!!! Those leaks are driving me crazy! How does one finds a memory leak in a garbage collected environment ??! Daniel Turini Nov. 2, 2002.
It still stops at the same spot.. Maybe theres something wrong with SetRTF.. weird, like the person said after your post, if im not reading the whole file the SetRTF wouldn't work because I wouldn't have a complete RTF string, so I'm guessing that this "read part" is probably working it's just not displaying the whole file... weird. Any way thanks everyone for helping me, im going to look deeper into my SetRTF function. And im going to look at the string before SetRTF gets called. Thanks again, Rob Whoever said nothing's impossible never tried slamming a revolving door!
-
Hello, This is a dumb question but I'm using the following to stream RTF from a .rtf file to a CRichEditCtrl.. The problem is that it's only reading in the 4096, I understand it's because my cBuf is only 4096.. Is there a way to read the whole file in with out having to declare the size of the cBuf char?? Heres my code..
// READ THE .RTF FILE IN.
CFile fileRead;
if(fileRead.Open(strFile,CFile::modeRead))
{
char cBuf[4096];
UINT uBytesRead;while(uBytesRead = fileRead.Read(cBuf, sizeof(cBuf)-1)) { cBuf\[uBytesRead\] = NULL; SetRTF(CString(cBuf)); } fileRead.Close();
}
Whoever said nothing's impossible never tried slamming a revolving door!
Question: What is SetRTF()? ================================ Useful links
-
Question: What is SetRTF()? ================================ Useful links
I use SetRTF to stream an RTF string into a RichEdit control or view.. It's declared like this..
// in the .h
void SetRTF(CString sRTF);
static DWORD CALLBACK CBStreamIn(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb);// in the .cpp
void CMyView::SetRTF(CString sRTF)
{
// Read the text in
EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = CBStreamIn;
es.dwCookie = (DWORD) &sRTF;
GetRichEditCtrl().StreamIn(SF_RTF | SFF_SELECTION, es);
}DWORD CALLBACK CMyView::CBStreamIn(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
CString *pstr = (CString *)dwCookie;
USES_CONVERSION;
if( pstr->GetLength() < cb )
{
*pcb = pstr->GetLength();
memcpy(pbBuff, T2CA((LPCTSTR)*pstr), *pcb );
pstr->Empty();
}
else
{
*pcb = cb;
memcpy(pbBuff, T2CA((LPCTSTR)*pstr), *pcb );
*pstr = pstr->Right( pstr->GetLength() - cb );
}return 0;
}
Rob Whoever said nothing's impossible never tried slamming a revolving door!