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

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(8)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜


            注意,所有的代碼,建立的是win32 console application 支持MFC,因為CString,只有MFC才支持
            4、 char*和CString的相互轉換

                  CString包含了3個值:指向某個數據緩沖區的指針、該緩沖區中有效地字符記數(它是不可存取的,是位于CString地址之下的一個隱藏區域)及一個緩沖區長度。有效字符數的大小可以使從0到該緩沖最大長度值減1之間的任何數(因為字符串結尾有一個NULL字符)

                  4.1 char*轉換為CString

                   ①直接賦值

                     ②利用格式化轉換

            #include "stdafx.h"
            #include 
            "CString.h"
            #ifdef _DEBUG
            #define new DEBUG_NEW
            #undef THIS_FILE
            static char THIS_FILE[] = __FILE__;
            #endif
            /////////////////////////////////////////////////////////////////////////////
            // The one and only application object
            CWinApp theApp;
            using namespace std;
            int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
            {
                
            int nRetCode = 0;
                
            // initialize MFC and print and error on failure
                if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
                
            {
                    
            // TODO: change error code to suit your needs
                    cerr << _T("Fatal Error: MFC initialization failed"<< endl;
                    nRetCode 
            = 1;
                }

                
            else
                
            {
                    
            // TODO: code your application's behavior here.
                    CString strHello;
                    strHello.LoadString(IDS_HELLO);
                    cout 
            << (LPCTSTR)strHello << endl;
                    CString strConvert;
                    TCHAR
            * p= _T("this is a chToString test ");
                    
            //要試驗的話,只需要去掉注釋和加上注釋
                    strConvert = p;//直接復制
                    
            //strConvert.Format("%s",p);//格式化
                    
            //注意,這里不能直接cout<<strConvert,輸出的會是一串數字
                    cout<<"strConvert="<<(LPCTSTR)strConvert<<endl;
                }

                
            return nRetCode;
            }

                  4.2 CString轉換為char*

                  ①強制類型轉換為LPCTSTR

                  ②使用strcpy

                  需要說明的是,strcpy(或可移植的_tcscpy)的第二個參數是const wchar_t* (Unicode)或const char* (ANSI),系統編譯器將會自動對其進行轉換。

            #include "stdafx.h"
            #include 
            "CString.h"
            #ifdef _DEBUG
            #define new DEBUG_NEW
            #undef THIS_FILE
            static char THIS_FILE[] = __FILE__;
            #endif
            /////////////////////////////////////////////////////////////////////////////
            // The one and only application object
            CWinApp theApp;
            using namespace std;
            int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
            {
                
            int nRetCode = 0;
                
            // initialize MFC and print and error on failure
                if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
                
            {
                    
            // TODO: change error code to suit your needs
                    cerr << _T("Fatal Error: MFC initialization failed"<< endl;
                    nRetCode 
            = 1;
                }

                
            else
                
            {
                    
            // TODO: code your application's behavior here.
                    CString strHello;
                    strHello.LoadString(IDS_HELLO);
                    cout 
            << (LPCTSTR)strHello << endl;
                    
            //強制類型轉換為LPCTSTR
                    CString theString( (_T("Char test ")));
                    LPTSTR lpsz
            =(LPTSTR)(LPCTSTR)theString;
                    
            //使用strcpy
                    LPTSTR lpsz1=new TCHAR[theString.GetLength()+1];
                    _tcscpy(lpsz,theString);
                }

                
            return nRetCode;
            }

                     4.3使用GetBuffer

                    如果需要修改CString中的內容,它有一個特殊的方法可以使用,那就是GetBuffer,它的作用是返回一個可寫的緩沖指針。如果只是打算修改字符或者截短字符串,例如

            CString theString( (_T("Char test ")));
            LPTSTR lpsz
            =s.GetBuffer();
            /*添加p的代碼*/
            s.ReleaseBuffer();
            //使用完后及時釋放


            如果還想獲得更多關于《Visual C++代碼參考與技巧大全》的內容,可點擊下面網址,

            http://www.shnenglu.com/kangnixi/archive/2010/01/13/105591.html
            posted on 2010-01-27 22:55 煙皚 閱讀(473) 評論(0)  編輯 收藏 引用 所屬分類: 《Visual C++代碼參考與技巧大全》學習筆記
            久久精品国产福利国产秒| 狠狠色丁香久久婷婷综合图片| 一本久久免费视频| 国色天香久久久久久久小说| 97久久国产亚洲精品超碰热 | 亚洲乱码日产精品a级毛片久久 | 国产成人精品久久免费动漫| 久久最新精品国产| 久久国内免费视频| 人妻无码中文久久久久专区| 久久国产免费| 亚洲级αV无码毛片久久精品 | 久久91亚洲人成电影网站| 久久综合久久鬼色| AAA级久久久精品无码片| 欧洲国产伦久久久久久久| 韩国免费A级毛片久久| 国产成人精品久久| 99久久精品午夜一区二区 | 国产亚洲美女精品久久久久狼| 好久久免费视频高清| 亚洲午夜久久久| 欧美久久综合性欧美| 72种姿势欧美久久久久大黄蕉 | 99久久国产主播综合精品| 久久这里只有精品18| 亚洲va国产va天堂va久久| 久久久久久国产a免费观看不卡| 色偷偷888欧美精品久久久| 精品国产日韩久久亚洲| 日产久久强奸免费的看| 久久久99精品成人片中文字幕| 中文字幕无码久久精品青草 | 99久久成人国产精品免费 | 久久婷婷国产麻豆91天堂| 天天爽天天狠久久久综合麻豆| 开心久久婷婷综合中文字幕| 激情久久久久久久久久| 国产高潮久久免费观看| 狠狠久久综合伊人不卡| 免费一级欧美大片久久网|