• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            kenlistian

            厚積薄發. 勤為槳,思為帆

               :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理 ::
              73 隨筆 :: 4 文章 :: 22 評論 :: 0 Trackbacks

                MFC中CEdit多行情況下,調用GetLine()時,發現老是后面跟有亂碼。即使你把str初始化也是如此。

            對于采用GetLine()調用,其函數說明有2種。

            1.int GetLine(
               int nIndex,
               LPTSTR lpszBuffer 
            ) const;
            2.int GetLine(
               int nIndex,
               LPTSTR lpszBuffer,
               int nMaxLength 
            ) const;

              代碼如下:

               char str[10] = {'\0'};
               int nLineNum;//想要獲取的行號
               nLineNum=0;
               m_ctlEditTest.GetLine(nLineNum,str);

            Remarks : The copied line does not contain a null-termination character.

            看了下msdn,一句話說得很明白,getline做了copy后是不給你加null結束符的。因此需要自己在定義的char字符串末尾添加。而一次實際上會copy回多少個字節。則在該函數的說明中,在msdn中清晰表達為:

            Return Value

              The number of bytes actually copied. The return value is 0 if the line number specified by nIndex is greater than the number of lines in the edit control.

            所以,上面的代碼段得用一個值取得實際長度,把多余的截取掉,但我在用getline1時老是返回0,不知道為何?不過改成getline2,指定copy9個字節時,同時對第10個字節設置為null則正常返回,沒有亂碼的出現。如下

              int iLen =  m_ctlEditTest.GetLine(nLineNum,str, 9);

              str[10] = '\0'; 

            即可。

            。。。。。。。

            在cedit中還有個linelength函數,

            int LineLength(
               int nLine = -1 
            ) const;

            該函數是應該返回指定行的長度,但是如果不仔細看msdn的說明,很容易誤解nLine是指cedit行中的某一行行數而代入其中,并得到錯誤的結論。

            而在msdn中,nLine的說明是:

            nLine

            Specifies the character index of a character in the line whose length is to be retrieved. If this parameter is –1, the length of the current line (the line that contains the caret) is returned, not including the length of any selected text within the line. When LineLength is called for a single-line edit control, this parameter is ignored.

            就是說nLine是字符串的字符索引,在多行情況下所以它必須通過LineIndex函數來獲取。在msdn中如下描述,

               Use the LineIndex member function to retrieve a character index for a given line number within a multiple-line edit control.

            而LineIndex函數說明如下:

            all this function to retrieve the character index of a line within a multiple-line edit control.

            int LineIndex(
               int nLine = -1 
            ) const;

            nLine

            Contains the index value for the desired line in the text of the edit control, or contains –1. If nLine is –1, it specifies the current line, that is, the line that contains the caret.

            該nLine才是真正的CEdit中的某一行列。也就是我們通過getlinecount()獲取到cedit的行數后,在到每一行中去找一個character index,再才能確定一行的長度。

            有時,覺得mfc是不是有點對個簡單的問題把彎子繞得太遠了點吧。

            。。。。

            下面摘抄另一種解決方案,采用CString方式來copy一行的長度,我在自己機子測試過,通過GetLine函數1沒有通過,返回的是個空串,而采用GetLine2指定返回一個最大長度時,卻能copy回所要的數據,不過當采用最大長度時,由于拷貝回來的是一個不帶null終結符的串,則存在亂碼。由于工作時間緊張,問題只能采用指定大小的char串處理,但是采用CString串如果不指定大小的方法做一個mark。下列代碼供測試參考。

            CString strTemp;
            int nLineNum;
            nLineNum=0;
            m_ctlEditTest.GetLine(nLineNum,strTemp.GetBufferSetLength(m_ctlEditTest.LineLength(m_ctlEditTest.LineIndex(nLineNum))));
            strTemp.ReleaseBuffer();

            。。。。。。

            總而言之,感覺vc中mfc太過于細節,莫免麻煩。但是感覺經歷過一次后,就不再是磕腳的石頭,而是心態的放心。也許,這是心理作用而已。

            posted on 2008-02-15 21:17 kenlistian 閱讀(5495) 評論(0)  編輯 收藏 引用
            www.久久热.com| 亚洲伊人久久综合影院| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | AV无码久久久久不卡网站下载 | 久久精品国产亚洲av麻豆图片| 久久人人爽人人爽人人片AV不| 久久九九精品99国产精品| 热久久这里只有精品| 热久久视久久精品18| 久久久一本精品99久久精品88| 色综合久久中文综合网| 狠狠色丁香久久婷婷综合| 国产精品久久久久aaaa| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 国产叼嘿久久精品久久| 精品久久亚洲中文无码| 亚洲精品高清久久| 无码人妻精品一区二区三区久久| 色成年激情久久综合| 色婷婷久久综合中文久久蜜桃av| 亚洲国产精品久久久久婷婷老年| 精品多毛少妇人妻AV免费久久 | 97精品国产97久久久久久免费| 久久久久久久久久久精品尤物| 久久精品中文字幕一区| 色综合久久中文字幕无码| 欧美亚洲国产精品久久久久| 激情综合色综合久久综合| 久久99国产精品二区不卡| 久久av无码专区亚洲av桃花岛| 伊人久久一区二区三区无码| 国産精品久久久久久久| 亚洲精品高清久久| 国产成人久久777777| 99久久99久久精品国产片果冻| 久久99热只有频精品8| 久久久久99精品成人片试看 | 国内精品久久久久久99蜜桃 | 99久久国产亚洲高清观看2024 | 狠狠色丁香久久婷婷综合蜜芽五月| 久久精品国产99久久久香蕉|