wxString and normal ANSI strings: How to copy?

Hi all.

I have the following script:

1
2
3
4
5
6
7
8
9
char      string1[256];
wxString  string2;

strcpy(string1, "Hello");

Now: copy string1 into string2

...


How can I copy string1 into string2? A 'strcpy(string2, string1)' does not work.
Thanks for some hints.
Last edited on
It does.
Well, not in my case:

1
2
3
4
5
6
7
8
   wxString wxDirectory;
   wxDirectory = event.GetPath();
   
   strcpy(filename, (const char*)wxDirectory.mb_str(wxConvUTF8) );
   
   Something_is_done_with_the_file_name_here(filename);
  
   strcpy(wxDirectory.mb_str,filename);


The gcc compilar says:

1
2
|294|error: argument of type ‘const wxCharBuffer (wxString::)(const wxMBConv&)const’ does not match ‘char*’|
||=== Build finished: 1 errors, 1 warnings ===|
You are abusing the wxString class.

1
2
3
4
5
6
7
8
9
char     filename[ MAX_PATH ];
wxString directory;

directory = event.GetPath();
strcpy( filename, directory.mb_str( wxConvUTF8 ) );

Something_is_done_with_the_file_name_here( filename );

directory = filename;

It is always a good idea to keep a reference at hand:
http://docs.wxwidgets.org/2.8/wx_wxstring.html

Hope this helps.
He does not like

directory = filename;

He says:

|294|error: ambiguous overload foroperator=’ in ‘directory = filename’|
Last edited on
Yes, according to the documentation Duoas posted, that operator isn't overloaded for char*. Read it and you'll see how you can do it.
directory = wxString::FromAscii(filename);
Right?
I guess. Never used wxWidgets myself.
Well, at least it worked. ;-)
Sorry, I never use it either...
But yes, the documentation and a compile or two will get you through it.
char string1[256];

This is an array of 256 characters; one char string.

wxString string2[256];

This is an array of 256 string objects; two hundred and fifty-six strings.


Do you actually intend to copy your one char string into two hundred and fifty-six wxStrings?

To create a wxstring from a char string, use the following:

1
2
3
const char* string1 = "Hello world";
// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed
wxString string2(sring1, wxConvUTF8);


To copy a char string into an already existing wxString, do much as Duoas describes.
Last edited on
Uuups, this was a typing error. I corrected it:

1
2
char      string1[256];
wxString  string2;

Topic archived. No new replies allowed.