Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VS2005 C++ MFC
Calling this function showing junk charecter as last item in an UNICODE type project
 
//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 14-Nov-11 1:24am
Selva K2.3K
Edited 14-Nov-11 1:25am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
selvapk at 14-Nov-11 22:41pm
   
Thank you , solved it
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try replacing
for(int i=0;i<=256;i++)
with
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)
  Permalink  
v2
Comments
Mehdi Gholam at 14-Nov-11 6:32am
   
Nice catch, 5'ed
Timberbird at 14-Nov-11 7:16am
   
Thank you. It appears the reason was not crossing buffer boundaries - see my edited answer or Richard's
selvapk at 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 at 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.
selvapk at 14-Nov-11 6:51am
   
how to stop displaying "쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌"
Richard MacCutchan at 14-Nov-11 7:04am
   
But what if the string is not null terminated, as in this case? See my answer below.
Timberbird at 14-Nov-11 7:13am
   
Thank you. I found out what OP problem actually is after his comment, and then went to msdn. Should have done it in the beginning, I guess, but that extra cycle could definitely be the reason :)
Richard MacCutchan at 14-Nov-11 7:27am
   
Well the extra cycle does not help, but the basic problem is that the returned string is not null terminated.

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 85
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 14 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100