• <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

            厚積薄發(fā). 勤為槳,思為帆

               :: 首頁(yè) :: 新隨筆 ::  :: 聚合  :: 管理 ::
              73 隨筆 :: 4 文章 :: 22 評(píng)論 :: 0 Trackbacks

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

            對(duì)于采用GetLine()調(diào)用,其函數(shù)說(shuō)明有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;//想要獲取的行號(hào)
               nLineNum=0;
               m_ctlEditTest.GetLine(nLineNum,str);

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

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

            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.

            所以,上面的代碼段得用一個(gè)值取得實(shí)際長(zhǎng)度,把多余的截取掉,但我在用getline1時(shí)老是返回0,不知道為何?不過(guò)改成getline2,指定copy9個(gè)字節(jié)時(shí),同時(shí)對(duì)第10個(gè)字節(jié)設(shè)置為null則正常返回,沒有亂碼的出現(xiàn)。如下

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

              str[10] = '\0'; 

            即可。

            。。。。。。。

            在cedit中還有個(gè)linelength函數(shù),

            int LineLength(
               int nLine = -1 
            ) const;

            該函數(shù)是應(yīng)該返回指定行的長(zhǎng)度,但是如果不仔細(xì)看msdn的說(shuō)明,很容易誤解nLine是指cedit行中的某一行行數(shù)而代入其中,并得到錯(cuò)誤的結(jié)論。

            而在msdn中,nLine的說(shuō)明是:

            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.

            就是說(shuō)nLine是字符串的字符索引,在多行情況下所以它必須通過(guò)LineIndex函數(shù)來(lái)獲取。在msdn中如下描述,

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

            而LineIndex函數(shù)說(shuō)明如下:

            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中的某一行列。也就是我們通過(guò)getlinecount()獲取到cedit的行數(shù)后,在到每一行中去找一個(gè)character index,再才能確定一行的長(zhǎng)度。

            有時(shí),覺得mfc是不是有點(diǎn)對(duì)個(gè)簡(jiǎn)單的問題把彎子繞得太遠(yuǎn)了點(diǎn)吧。

            。。。。

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

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

            。。。。。。

            總而言之,感覺vc中mfc太過(guò)于細(xì)節(jié),莫免麻煩。但是感覺經(jīng)歷過(guò)一次后,就不再是磕腳的石頭,而是心態(tài)的放心。也許,這是心理作用而已。

            posted on 2008-02-15 21:17 kenlistian 閱讀(5484) 評(píng)論(0)  編輯 收藏 引用

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久精品国产一区二区三区日韩| 久久乐国产精品亚洲综合| 久久免费大片| 国产AⅤ精品一区二区三区久久| 丰满少妇高潮惨叫久久久| 少妇高潮惨叫久久久久久| 欧美丰满熟妇BBB久久久| 午夜精品久久久久久毛片| 久久久噜噜噜久久熟女AA片 | 蜜臀久久99精品久久久久久| 久久久久亚洲爆乳少妇无| 国产精品热久久毛片| 国产99久久久久久免费看| 久久五月精品中文字幕| 亚洲综合日韩久久成人AV| 久久久久99精品成人片直播| 91麻豆精品国产91久久久久久 | 国产精品免费久久久久久久久| 久久久久女教师免费一区| 欧美久久久久久精选9999| 久久精品国产亚洲av麻豆蜜芽| 国产一级做a爰片久久毛片| 久久99精品久久久久久齐齐| 亚洲va久久久噜噜噜久久男同 | 久久国产一区二区| 久久久久无码中| 久久久噜噜噜久久熟女AA片 | 国产呻吟久久久久久久92| 久久久久精品国产亚洲AV无码 | 97久久天天综合色天天综合色hd | 国产69精品久久久久99尤物| 精品国产乱码久久久久软件| 996久久国产精品线观看| 伊人久久无码精品中文字幕| 欧美久久精品一级c片片| 国产免费久久精品99re丫y| 久久精品国产久精国产| 精品久久久久久久久免费影院| 国产精品VIDEOSSEX久久发布| 99久久婷婷国产综合亚洲| 亚洲国产日韩综合久久精品|