I first thought to use:
Code: Select all
const wxCharBuffer wxString::mb_str ( const wxMBConv & conv = wxConvLibc ) const
because it seemed like a nice one-line copy from a wxString to a wxCharBuffer. But then, how to I copy the wxCharBuffer into the character array? Or even get a single character from a wxCharBuffer? I don't see any conversion methods on http://docs.wxwidgets.org/3.0/classwx_char_buffer.html. So that idea is out.
Then I thought to try converting one character at a time, using GetChar. Here is that code:
Code: Select all
#include "ut_bts.h" // 2i
#include "ut_gui.h" // 2j
#include "Settings_Class.h" // 5r
class Settings_Class; // 5s
#include "Wrapper.h" // 5w
#include <wx/buffer.h> // 6
#include <wx/filename.h> // 6
#include <wx/msgdlg.h> // 6
#include <wx/string.h> // 6
// | ut_gui_Set_Char_Array_to_wxSt |
// | |
// | char array = wxString |
// | |
// | Description: This procedure sets the incoming C-style character array |
// | to the contents of incoming wxString. |
(char ch_array [gd_bts_FILENAME_LENGTH],
char ch = ' ';
int i = 0;
int Max_Length = 0;
size_t Length_of_wxSt = 0;
// Initialize the target character array to end-of-string characters.
memset (&ch_array, '\0', gd_bts_FILENAME_LENGTH);
Length_of_wxSt = wx_St.Length (); // <-- GRRR! Execution dies here.
if (Length_of_wxSt == 0) // Incoming wxString is
; // empty. Just leave.
if (Length_of_wxSt < (size_t) gd_bts_FILENAME_LENGTH) // Set range limit.
Max_Length = Length_of_wxSt;
Max_Length = gd_bts_FILENAME_LENGTH;
for (i = 0; i < Max_Length; i++) // Aaaand...copy!
ch = (char) wx_St.GetChar (i);
ch_array [i] = ch;
} // ut_gui_Set_Char_Array_to_wxSt
The code compiles and links just fine. But when I stepped through execution, and I hit the line--
Length_of_wxSt = wx_St.Length ();
--execution crashes with a "Program Received signal SIGSEGV / Stack Trace is available in the 'Call Stack' Tab" error message. I didn't get any more details except for two links to lines in string.h that I don't understand.
At first I thought wx_St was empty. But no: I have a "Watch" on wx_St showing its value during ut_gui_Set_Char_Array_to_wxSt's execution. When ut_gui_Set_Char_Array_to_wxSt is called, wx_St's value is "F:\Temp\default.csv".
So I tried other things:
* changing wx_St's declaration in the argument list from
wxString * wx_St
and changed all the affected code appropriately. I still got SIGSEGV errors. I changed the code back, because I didn't want to risk modifying wx_St.
* creating a local wxString temp_wx_St, setting it to wx_St, and using temp_wx_St instead of wx_St. I got the same SIGSEGV error when execution hit the assignment to set temp_wx_St to wx_St.
* setting the local wxString temp_wx_St as follows:
temp_wx_St = wx_St.Capitalize ();
I got the SIGSEGV error.
The more things I tried, the more it seemed like NO MATTER HOW I access the incoming argument wx_St the first time--whether in a .GetChar or .Capitalize or .Clone--I get that SIGSEGV error.
SURELY COPYING A WXSTRING INTO A CHARACTER ARRAY IS NOT A NEW PROBLEM. It should be easier than this!!!
I do not have my heart set on following this approach. What do other people do copy a wxString into a C-type character array?
I am using:
Windows 10, 64 bits