Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

granat

C++ Builder problem (Edit-box) !!

This topic is 6950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an EditBox called Edit1. I use this Edit1 to type in a string. I want this string to be copied into my variable char temp[256]; The problem is that an EditBox contains AnsiString. To convert I try this but it does not work: char temp[256]; strcpy (temp, Form1->Edit1->Text.c_str()); Why does it not work ???

Share this post


Link to post
Share on other sites
Advertisement
Remember, a ''C string'' is a NULL-terminated array of characters. In this case, the array is probably only 1 character long, and is the single character zero to represent the fact that there are no characters in the string. Your strcpy operation then tries to write your string in temp (which could be anything up to 256 characters long - or more, if you messed up ) over the buffer assigned to Edit1''s Ansistring. This would be wrong as you''re writing on memory not allocated to you. Therefore you need to use one of the Ansistring''s functions to do this properly, and get it to allocate the memory and assign it itself. You''ll kick yourself, but this -should- work:
Edit1->Text = temp;
As the AnsiString class has an = operator which accepts another AnsiString, and there is a constructor which takes your pointer to char and makes the AnsiString out of it.

Share this post


Link to post
Share on other sites

I don´t follow you.

I don´t want an AnsiString. I want the value of Edit1 to be copied into my char temp[256] variable.

I thought the function c_str() would return the content of Edit1 as a "normal" c-null terminated string...

But it did not...

BTW: You seem pretty smart Kylotan. What do you do for a living ??

Share this post


Link to post
Share on other sites
try this
char mycstr[256];
AnsiString temp=Form1->Edit1->Text;
for (int t=0;t<temp.Length();t++)
{ mycstr[t]=temp[t]; }
mycstr[t]='\0';

i'm not sure if it's gonna work. but it's highly possible to give u the right answer in "mycstr"

- pouya


Edited by - pouya on 5/2/00 12:10:31 AM

Share this post


Link to post
Share on other sites
quote:
Original post by granat


I don´t follow you.

I don´t want an AnsiString. I want the value of Edit1 to be copied into my char temp[256] variable.


See, I completely misread your question. Sorry! My excuse: I don''t use strcpy() often

quote:

I thought the function c_str() would return the content of Edit1 as a "normal" c-null terminated string...

But it did not...


It should do, I believe. I am as confused as you. Sadly I am not at a computer with Builder on it so I can''t find out more right now. When you say "it doesn''t work", what actually happens?

Although pouya''s method seems long-winded, if it works, it''s better than something that doesn''t work Let us know what happens when you try that.

quote:
BTW: You seem pretty smart Kylotan. What do you do for a living ??


So smart I answered a question you didn''t even ask

I do websites with Active Server Pages, Access Databases, VBScript, etc etc. Totally MS-based. I hate it!

If anybody in the UK wants to offer me a game-programming related (paying) job, or anything more interesting than the above, please email me!!

Share this post


Link to post
Share on other sites
I did what poyan suggested but the program crashed.
the offending line was:

mycstr[t]=temp[t];

if I changed it to this it did not crash:
mycstr[t]= 'a';

I guess an ansistring can not be used as an array.


Edited by - granat on 5/2/00 11:44:58 AM

Share this post


Link to post
Share on other sites
ya, i made a mistake. it''s not temp[t], it''s temp.CharAt(t);
sorry

- pouya

Share this post


Link to post
Share on other sites
I got it to work...
I did nothing special...

AnsiString A;
A = Form_BuildResource->Edit_BuildFileName->Text;
strcpy(temp, A.c_str());

I am sure I have tried this before...Weird...Well thanks
for your help.......bye bye..

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Heres another facile solution. C++Builder gives you a function called "StrPCopy()" which can write AnsiString to buffer. Have you tried it ?

Kwanji

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!