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.cppblog.com/kangnixi/archive/2010/01/13/105591.html
posted on 2010-01-27 22:55 烟皑 阅读(450) 评论(0)  编辑 收藏 引用 所属分类: 《Visual C++代码参考与技巧大全》学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理