CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

CString怎样转化成char?

楼主yung(原来我还什么也不会)2002-03-13 13:29:25 在 VC/MFC / 基础类 提问

CString怎样转化成char? 问题点数:20、回复次数:6Top

1 楼Wugifer()回复于 2002-03-13 13:33:11 得分 0

LPCSTRTop

2 楼zzq_fortu(郑)回复于 2002-03-13 13:42:17 得分 0

CString::operator   LPCTSTR  
  operator   LPCTSTR   (   )   const;  
   
  Return   Value  
   
  A   character   pointer   to   the   string’s   data.  
   
  Remarks  
   
  This   useful   casting   operator   provides   an   efficient   method   to   access   the   null-terminated   C   string   contained   in   a   CString   object.   No   characters   are   copied;   only   a   pointer   is   returned.   Be   careful   with   this   operator.   If   you   change   a   CString   object   after   you   have   obtained   the   character   pointer,   you   may   cause   a   reallocation   of   memory   that   invalidates   the   pointer.  
   
  Example  
   
  The   following   example   demonstrates   the   use   of   CString::operator   LPCSTR.  
   
  //   If   the   prototype   of   a   function   is   known   to   the   compiler,  
  //   the   LPCTSTR   cast   operator   may   be   invoked   implicitly  
   
  CString   strSports(_T("Hockey   is   Best!"));  
  TCHAR   sz[1024];  
   
  lstrcpy(sz,   strSports);  
   
  //   If   the   prototype   isn't   known,   or   is   a   va_arg   prototype,  
  //   you   must   invoke   the   cast   operator   explicitly.   For   example,  
  //   the   va_arg   part   of   a   call   to   sprintf()   needs   the   cast:  
   
  sprintf(sz,   "I   think   that   %s!\n",   (LPCTSTR)   strSports);  
   
  //   while   the   format   parameter   is   known   to   be   an   LPCTSTR   and  
  //   therefore   doesn't   need   the   cast:  
   
  sprintf(sz,   strSports);  
   
  //   Note   that   some   situations   are   ambiguous.   This   line   will  
  //   put   the   address   of   the   strSports   object   to   stdout:  
   
  cout   <<   strSports;  
   
  //   while   this   line   will   put   the   content   of   the   string   out:  
   
  cout   <<   (LPCTSTR)   strSports;  
   
  摘自MSDNTop

3 楼alfwolf(木马煞)回复于 2002-03-13 13:46:56 得分 0

(LPCTSTR)str;Top

4 楼alfwolf(木马煞)回复于 2002-03-13 13:47:54 得分 0

(LPCTSTR)str;Top

5 楼orangetouch(orange)回复于 2002-03-13 13:51:09 得分 0

或者是CString::GetBuffer()Top

6 楼yung(原来我还什么也不会)回复于 2002-03-13 13:53:18 得分 0

CString是要打开位图文件的路径名,用fopen(char*,"rb")来读位图信息,就是不能转化成char*,如果:  
  int   n   =CString.GetPathName().getLength().  
  char   *   t   =   new   char[n];  
  for(int   j   =0;j<n;j++)  
  t[j]   =   CString.GetPathName().getat(j);  
  读出来得t后面多出四个空格,fopen()就打不开文件了,怎么办????  
  Top

7 楼ymll(扬眉)回复于 2002-03-13 13:55:36 得分 20

(LPCSTR)(char*)strTop

8 楼Lava_sdb(大波)回复于 2002-03-13 15:13:16 得分 0

1。此CString并非指vc中的CString类,而是文件的路径名  
  ??????  
  1。既然以得路径名,直接用   fopen(...)打开就行了!  
  2。CString.GetPathName()中的GetPathName()是什么?  
   
  在vc中直接用CFile类对文件操作,更简单!  
  相关请看MSDNTop

相关问题

  • 怎样把char型转化为CString型
  • 怎样将CString类型转化为char*类型
  • 怎样把一个char*转化成一个CString
  • char 型数组怎样转化为 CString 型数据?
  • CString 型数据怎样转化为char 型数组 ?
  • string怎样转化为char* (注意不是CString)
  • 怎样将CString类型的变量转化成char型数组呢?
  • CString 与char的转化问题
  • 如何将CString转化为char*类型?
  • char 与 CString转化的问题。

关键词

  • cstring
  • sz
  • cast
  • operator
  • prototype
  • pointer
  • character

得分解答快速导航

  • 帖主:yung
  • ymll

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo