关于ANSISTRING 和char[]之间的转换。

CPerlAsm_Lx 2003-11-03 08:06:22
char strSrc[]="c:\\a\\*.txt\0";
//这个是一个函数里要用到的参数,后面还有个\0
我用Edit1->Text.c_str(); 怎么转换不过去呢。
(Edit1->Text+"\0").c_str();也转换不过去。

怎么转换呢。
...全文
247 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
spotless 2003-11-04
  • 打赏
  • 举报
回复
char *strSrc;
strSrc=new char[Edit1->text.Length()+1];
strcpy(strSrc,Edit1->text.c_str());
strSrc[Edit1->text.Length()]='\0';
zcchm 2003-11-04
  • 打赏
  • 举报
回复
“char *ch = Test.c_str(); ”
这段代码是非常错误的!
参考下面帮助

Returns the AnsiString converted to null-terminated character array.

char* __fastcall c_str() const;

Description

c_str returns a pointer a null-terminated character array containing the same characters as the AnsiString. If the AnsiString is unassigned, c_str returns an empty string (摂).

Usually, the value returned by c_str points to the internal character array referenced by the data property. This pointer is valid until the string is next modified (for example when the SetLength method is called or the AnsiString goes out of scope). However, if the internal array is NULL, c_str returns a pointer to a string literal.

The c_str method is intended primarily for reading the value of the AnsiString. To modify the string抯 value, use the [] operator or AnsiString methods such as Insert and Delete.
pp616 2003-11-04
  • 打赏
  • 举报
回复
AnsiString str="hello";
char *buf;
buf=(char *)malloc(str.Length()+1);
memset(buf,0x00,str.Length()+1);
strcpy(buf,str.c_str());
woxihuanbohe 2003-11-03
  • 打赏
  • 举报
回复
AnsiString Test = "c:\\a\\*.txt";
char *cp = Test.c_str();
CPerlAsm_Lx 2003-11-03
  • 打赏
  • 举报
回复
相反。
我想做的是把EDIT里的东西喜欢换成CHAR[],但是这个CHAR[]要求带一个 \0

老是转换失败。我给大家把代码铁上来

int nOk;
// char strSrc[]="c:\\a\\*.txt\0";
//可以改变源路径
// char strDst[]="c:\\b\0";
//设置目的路径
char strTitle[]="File copying";
//进度题头
SHFILEOPSTRUCT FileOp;
FileOp.hwnd=Handle;
FileOp.wFunc=FO_COPY;
//执行文件拷贝
FileOp.pFrom= Edit1->Text.c_str() ;
FileOp.pTo= Edit2->Text.c_str();
FileOp.fFlags=FOF_ALLOWUNDO;
FileOp.hNameMappings=NULL;
FileOp.lpszProgressTitle=strTitle;

nOk=SHFileOperation(&FileOp);
if(nOk)
ShowMessage("There is an error: %d\n");
else
ShowMessage("SHFileOperationfinished successfully\n");

if(FileOp.fAnyOperationsAborted)
ShowMessage("Operation was aborted!\n");

//也就说原来用注释掉的那个CHAR[]是可以的。可是我想从EDIT里读取信息给程序啊。但是EDIT里的ANSISTRING转换成CHAR[]以后。程序说找不到什么什么盘。。。
52vc 2003-11-03
  • 打赏
  • 举报
回复
AnsiString(Edit1->Text);
「已注销」 2003-11-03
  • 打赏
  • 举报
回复
不知道你要怎么转换?是不是转换成AnsiString
如果是:
AnsiString dd=strSrc;应该可以!\0是字符串结束标志。
yjy1001 2003-11-03
  • 打赏
  • 举报
回复
char *ch = Test.c_str();

char * to AnsiString

char * ch;
AnsiString str;
str=StrPas(ch);

AnsiString to char *
char * ch;
AnsiString str;
ch=str.c_str();

/0 是字符串的结束符号。
你可以这样赋值进去
CHAR[0]="c";
CHAR[1]=":";
CHAR[2]="\";
CHAR[3]="\";
CHAR[4]="a";
CHAR[5]="\";
CHAR[6]="\";
CHAR[7]="*";
CHAR[8]=".";
CHAR[9]="t";
CHAR[10]="x";
CHAR[11]="t";
CHAR[0]=0;// “/0”==NULL=0(AscII 码值)


13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧