Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Calling this function showing junk charecter as last item in an UNICODE type project

C#
//Member Function to Read Data From File And Store it in List Control
void CDataFunction::WriteFunction(CListCtrl* pListCtrl,CString* m_pFileName)
{
    m_VFlag=TRUE;
    m_VNumlist=1;
    m_VFileName = (m_pFileName->GetString());
    //Clear All Data In List Control
    pListCtrl->DeleteAllItems();
    WCHAR m_VStrBuf[256],m_VTemp[100];
    //Read All The Data in File and Store in a Buffer
    GetPrivateProfileString(_T("List1"),NULL,NULL,m_VStrBuf,256,m_VFileName);
    //Insert the Read Data into The List Control
    for(int i=0;i<=256;i++)
    {
        if(m_VStrBuf[i]!='\0')
        {
            m_VFlag=TRUE;
            for(int j=0;m_VFlag==TRUE;j++)
            {
                m_VTemp[j]=m_VStrBuf[i+j];
                if(m_VStrBuf[i+j]=='\0')
                {
                    m_VTemp[j+1] ='\0';
                    pListCtrl->InsertItem(m_VNumlist,m_VTemp);
                    m_VNumlist++;
                    m_VFlag=FALSE;
                    i=i+j;
                }
            }
        }
    }
}
Posted
Updated 14-Nov-11 0:25am
v2

The GetPrivateProfileString() function returns the number of characters stored in the buffer, which you have ignored. Capture that value and use it as the string limit rather than assuming that you will receive a null terminated string.
 
Share this answer
 
Comments
Selva K 14-Nov-11 22:41pm    
Thank you , solved it
Try replacing
C++
for(int i=0;i<=256;i++)

with
C++
for(int i=0;i<256;i++)



----- EDIT -----

Your comment implies that you actually want to distinct returned values from junk. Then you should consider this function return value, as described it its specification[^]. It can also tell you when buffer provided is too small (so 256 bytes are not enough)
 
Share this answer
 
v2
Comments
Mehdi Gholam 14-Nov-11 6:32am    
Nice catch, 5'ed
Timberbird 14-Nov-11 7:16am    
Thank you. It appears the reason was not crossing buffer boundaries - see my edited answer or Richard's
Selva K 14-Nov-11 6:51am    
It has the buffer size of 256 and its not running beyond 256
but its not fully occupied it showing the words and remaing as junk words only within 256
eg,it has m_VStrBuf="selva0aaaa0bbbbb00쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌"
in list displayed as selva,aaaa,bbbbb,쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌
Stefan_Lang 14-Nov-11 9:09am    
m_VStrBuf[256] will access the 257th char, not the 256th as you seem to believe. Remember that indexing starts at 0, not at 1. That's what Timberbird referred to.

However, as Richard pointed out in solution 2, the real problem is that you keep looking for strings past the end of the last actual value. You must check the function return to find out where the actual end of the list is in your array and only look up to that point.
Selva K 14-Nov-11 6:51am    
how to stop displaying "쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900