Anybody can help?

[b][red]This message was edited by peachieboy at 2003-10-13 4:40:58[/red][/b][hr]
Hi guys, could anybody please share with me if they have ever enounter this situation.

I have this software which opens a file in windows environment(MFC). i'm using the CStdio class. But the funny thing is it takes a very long time to read a file. I'm doing it line by line. For example to open a file consisting of arond 10,000 lines. (its a large file) it takes about 40 over secs.

But the amazing thing is that under DOS c programming environment, it barely takes more than 2 secs....anybody can advise?

############################################################
### this is the code from my program
#############################################################
if (fileEx.Open(m_sFilePath, CFile::modeRead | CFile::typeText))
{
FileTitle=fileEx.GetFileTitle();


while(fileEx.ReadString(sLine))
{
m_pgDemo.StepIt();

sText += sLine + sNEWLINE;

}
// Set lines in edit
m_editText.SetWindowText(sText);
m_pgDemo.SetPos(10000);
m_pgDemo.SetPos(0);

fileEx.Close();
}

###########################################################
#this is the ReadString function
#############################################################

BOOL CStdioFileEx::ReadString(CString& rString)
{
const int nMAX_LINE_CHARS = 4096;
BOOL bReadData;
LPTSTR lpsz;
int nLen = 0; //, nMultiByteBufferLength = 0, nChars = 0;
CString sTemp;
wchar_t* pszUnicodeString = NULL;
char * pszMultiByteString= NULL;

// If at position 0, discard byte-order mark before reading
if (!m_pStream || (GetPosition() == 0 && m_bIsUnicodeText))
{
wchar_t cDummy;
// Read(&cDummy, sizeof(_TCHAR));
Read(&cDummy, sizeof(wchar_t));
}

// If compiled for Unicode
#ifdef _UNICODE
// Do standard stuff -- both ANSI and Unicode cases seem to work OK
bReadData = CStdioFile::ReadString(rString);
#else

if (!m_bIsUnicodeText)
{
// Do standard stuff -- read ANSI in ANSI
bReadData = CStdioFile::ReadString(rString);
}
else
{
pszUnicodeString = new wchar_t[nMAX_LINE_CHARS];
pszMultiByteString= new char[nMAX_LINE_CHARS];

// Read as Unicode, convert to ANSI
fgetws(pszUnicodeString, nMAX_LINE_CHARS, m_pStream);
if (GetMultiByteStringFromUnicodeString(pszUnicodeString, pszMultiByteString, nMAX_LINE_CHARS))
{
rString = (CString)pszMultiByteString;
}

if (pszUnicodeString)
{
delete pszUnicodeString;
}

if (pszMultiByteString)
{
delete pszMultiByteString;
}
}
#endif

// Then remove end-of-line character if in Unicode text mode
if (bReadData)
{
// Copied from FileTxt.cpp but adapted to Unicode and then adapted for end-of-line being just '
'.
nLen = rString.GetLength();
if (nLen > 1 && rString.Mid(nLen-2) == sNEWLINE)
{
rString.GetBufferSetLength(nLen-2);
}
else
{
lpsz = rString.GetBuffer(0);
if (nLen != 0 && (lpsz[nLen-1] == _T('
') || lpsz[nLen-1] == _T('
')))
{
rString.GetBufferSetLength(nLen-1);
}
}
}

return bReadData;
}


Comments

  • [b][red]This message was edited by dwccgc at 2003-10-13 6:5:2[/red][/b][hr]
    [b][red]This message was edited by dwccgc at 2003-10-13 6:4:8[/red][/b][hr]
    CStdioFile in not designed for speed.
    If you look at the CStdioFileEx::ReadString(CString& rString) method you will see
    ..
    ..
    ..
    CString sTemp;

    This variable sTemp is created and destroyed, about 10,000 times for your program, everytime you read a line.

    Try using iostream.




    : [b][red]This message was edited by peachieboy at 2003-10-13 4:40:58[/red][/b][hr]
    : Hi guys, could anybody please share with me if they have ever enounter this situation.
    :
    : I have this software which opens a file in windows environment(MFC). i'm using the CStdio class. But the funny thing is it takes a very long time to read a file. I'm doing it line by line. For example to open a file consisting of arond 10,000 lines. (its a large file) it takes about 40 over secs.
    :
    : But the amazing thing is that under DOS c programming environment, it barely takes more than 2 secs....anybody can advise?
    :
    : ############################################################
    : ### this is the code from my program
    : #############################################################
    : if (fileEx.Open(m_sFilePath, CFile::modeRead | CFile::typeText))
    : {
    : FileTitle=fileEx.GetFileTitle();
    :
    :
    : while(fileEx.ReadString(sLine))
    : {
    : m_pgDemo.StepIt();
    :
    : sText += sLine + sNEWLINE;
    :
    : }
    : // Set lines in edit
    : m_editText.SetWindowText(sText);
    : m_pgDemo.SetPos(10000);
    : m_pgDemo.SetPos(0);
    :
    : fileEx.Close();
    : }
    :
    : ###########################################################
    : #this is the ReadString function
    : #############################################################
    :
    : BOOL CStdioFileEx::ReadString(CString& rString)
    : {
    : const int nMAX_LINE_CHARS = 4096;
    : BOOL bReadData;
    : LPTSTR lpsz;
    : int nLen = 0; //, nMultiByteBufferLength = 0, nChars = 0;
    : CString sTemp;
    : wchar_t* pszUnicodeString = NULL;
    : char * pszMultiByteString= NULL;
    :
    : // If at position 0, discard byte-order mark before reading
    : if (!m_pStream || (GetPosition() == 0 && m_bIsUnicodeText))
    : {
    : wchar_t cDummy;
    : // Read(&cDummy, sizeof(_TCHAR));
    : Read(&cDummy, sizeof(wchar_t));
    : }
    :
    : // If compiled for Unicode
    : #ifdef _UNICODE
    : // Do standard stuff -- both ANSI and Unicode cases seem to work OK
    : bReadData = CStdioFile::ReadString(rString);
    : #else
    :
    : if (!m_bIsUnicodeText)
    : {
    : // Do standard stuff -- read ANSI in ANSI
    : bReadData = CStdioFile::ReadString(rString);
    : }
    : else
    : {
    : pszUnicodeString = new wchar_t[nMAX_LINE_CHARS];
    : pszMultiByteString= new char[nMAX_LINE_CHARS];
    :
    : // Read as Unicode, convert to ANSI
    : fgetws(pszUnicodeString, nMAX_LINE_CHARS, m_pStream);
    : if (GetMultiByteStringFromUnicodeString(pszUnicodeString, pszMultiByteString, nMAX_LINE_CHARS))
    : {
    : rString = (CString)pszMultiByteString;
    : }
    :
    : if (pszUnicodeString)
    : {
    : delete pszUnicodeString;
    : }
    :
    : if (pszMultiByteString)
    : {
    : delete pszMultiByteString;
    : }
    : }
    : #endif
    :
    : // Then remove end-of-line character if in Unicode text mode
    : if (bReadData)
    : {
    : // Copied from FileTxt.cpp but adapted to Unicode and then adapted for end-of-line being just '
    '.
    : nLen = rString.GetLength();
    : if (nLen > 1 && rString.Mid(nLen-2) == sNEWLINE)
    : {
    : rString.GetBufferSetLength(nLen-2);
    : }
    : else
    : {
    : lpsz = rString.GetBuffer(0);
    : if (nLen != 0 && (lpsz[nLen-1] == _T('
    ') || lpsz[nLen-1] == _T('
    ')))
    : {
    : rString.GetBufferSetLength(nLen-1);
    : }
    : }
    : }
    :
    : return bReadData;
    : }
    :
    :
    :





Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion