Discussion:
wxString and c_str/char_str
(too old to reply)
Karl Karpfen
2014-10-15 12:30:25 UTC
Permalink
Hi,

I thought I did understand the changes in wxString related to c_str(), but
it seems I'm wrong.

Within 2.8 I used a statement like this:

path=wxString::Format(_T("%s%dR"),m_project->m_undoPath->c_str(),m_project->m_redoSteps->size());

As far as I remember the Linux-Version of wxWidgets required the c_str()
for m_undoPath, without I got a crash. Now with 3.0 I changed this line to

path=wxString::Format(_T("%s%dR"),m_project->m_undoPath->char_str(),m_project->m_redoSteps->size());

but this results in an compiler error. So what is the correct way of
formatting a wxString this way with using an other wxString?

Thanks!
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Eran Ifrah
2014-10-15 12:39:47 UTC
Permalink
For wx3.0 and later, the wxString::Format accepts strings directly:

wxString str = "something";
wxString result = wxString::Format("%s", str);

Eran
Post by Karl Karpfen
Hi,
I thought I did understand the changes in wxString related to c_str(), but
it seems I'm wrong.
path=wxString::Format(_T("%s%dR"),m_project->m_undoPath->c_str(),m_project->m_redoSteps->size());
As far as I remember the Linux-Version of wxWidgets required the c_str()
for m_undoPath, without I got a crash. Now with 3.0 I changed this line to
path=wxString::Format(_T("%s%dR"),m_project->m_undoPath->char_str(),m_project->m_redoSteps->size());
but this results in an compiler error. So what is the correct way of
formatting a wxString this way with using an other wxString?
Thanks!
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
or visit http://groups.google.com/group/wx-users
--
Eran Ifrah,
Author of codelite, a cross platform open source C/C++ IDE:
http://www.codelite.org
CodeLite IDE Blog: http://codeliteide.blogspot.com/
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Vadim Zeitlin
2014-10-15 12:43:26 UTC
Permalink
On Wed, 15 Oct 2014 14:30:25 +0200 Karl Karpfen wrote:

KK> Within 2.8 I used a statement like this:
KK>
KK> path=wxString::Format(_T("%s%dR"),m_project->m_undoPath->c_str(),m_project->m_redoSteps->size());
KK>
KK> As far as I remember the Linux-Version of wxWidgets required the c_str()
KK> for m_undoPath, without I got a crash. Now with 3.0 I changed this line to
KK>
KK> path=wxString::Format(_T("%s%dR"),m_project->m_undoPath->char_str(),m_project->m_redoSteps->size());
KK>
KK> but this results in an compiler error. So what is the correct way of
KK> formatting a wxString this way with using an other wxString?

Just do

path=wxString::Format("%s%dR",m_project->m_undoPath,m_project->m_redoSteps->size());

With all wx functions this is all you need.

Now if you have to use non-wx vararg functions, such as the real
printf(), and you really can't use wxPrintf(), then it's uglier as you need
to explicitly cast to the string type you want, e.g.

wxString s;
wxPrintf("%s", s); // simplest, best, use if possible
printf("%s", static_cast<const char*>(s.mb_str())); // if you have to
wprintf("%s", static_cast<const wchar_t*>(s.wc_str()));

Above you could use c_str() instead of mb_str() or wc_str() but I think
it's better to be explicit. And in any case you do need the cast -- this is
the price to pay for the things just working without any ".x_str()" in wx
itself.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Karl Karpfen
2014-10-20 12:30:11 UTC
Permalink
OK, it does not to work. To exclude influence of other parameters I tested
using following code:

path=wxString::Format(_T("%s"),m_project->m_undoPath);

m_undoPath contains a valid string

L"C:\\Users\\KARPFE~1\\AppData\\Local\\Temp\\45FE.tmp"

Bu this line still causes an assert "wxArgNormalizer(): format specifier
doesn't match argument type" and returns crap in "path".

I'm running wxWidgets 3 in x64 build.
Post by Karl Karpfen
KK>
KK>
path=wxString::Format(_T("%s%dR"),m_project->m_undoPath->c_str(),m_project->m_redoSteps->size());
KK>
KK> As far as I remember the Linux-Version of wxWidgets required the c_str()
KK> for m_undoPath, without I got a crash. Now with 3.0 I changed this line to
KK>
KK>
path=wxString::Format(_T("%s%dR"),m_project->m_undoPath->char_str(),m_project->m_redoSteps->size());
KK>
KK> but this results in an compiler error. So what is the correct way of
KK> formatting a wxString this way with using an other wxString?
Just do
path=wxString::Format("%s%dR",m_project->m_undoPath,m_project->m_redoSteps->size());
With all wx functions this is all you need.
Now if you have to use non-wx vararg functions, such as the real
printf(), and you really can't use wxPrintf(), then it's uglier as you need
to explicitly cast to the string type you want, e.g.
wxString s;
wxPrintf("%s", s); // simplest, best, use if possible
printf("%s", static_cast<const char*>(s.mb_str())); // if you have to
wprintf("%s", static_cast<const wchar_t*>(s.wc_str()));
Above you could use c_str() instead of mb_str() or wc_str() but I think
it's better to be explicit. And in any case you do need the cast -- this is
the price to pay for the things just working without any ".x_str()" in wx
itself.
Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Vadim Zeitlin
2014-10-20 13:27:40 UTC
Permalink
On Mon, 20 Oct 2014 05:30:11 -0700 (PDT) Karl Karpfen wrote:

KK> OK, it does not to work. To exclude influence of other parameters I tested
KK> using following code:
KK>
KK> path=wxString::Format(_T("%s"),m_project->m_undoPath);
KK>
KK> m_undoPath contains a valid string
KK>
KK> L"C:\\Users\\KARPFE~1\\AppData\\Local\\Temp\\45FE.tmp"
KK>
KK> Bu this line still causes an assert "wxArgNormalizer(): format specifier
KK> doesn't match argument type" and returns crap in "path".
KK>
KK> I'm running wxWidgets 3 in x64 build.

As usual, "extraordinary claims require extraordinary evidence". If you
indeed think that something as basic as this, and used by millions of
people, doesn't work, please provide some evidence for this, e.g. a minimal
patch to the minimal wxWidgets sample showing the problem.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Alec Teal
2014-10-21 22:38:58 UTC
Permalink
If this IS a problem, which I seriously doubt (GIGO - crap in crap out)
can you make it so we can reproduce it? Give me some code that causes this!

You've given us ONE LINE and not even the compiler's diagnostics! More
information please!

Failing that Vadim is completely right, you must just assume you are
cursed or something.

Alec
KK> OK, it does not to work. To exclude influence of other parameters I tested
KK>
KK> path=wxString::Format(_T("%s"),m_project->m_undoPath);
KK>
KK> m_undoPath contains a valid string
KK>
KK> L"C:\\Users\\KARPFE~1\\AppData\\Local\\Temp\\45FE.tmp"
KK>
KK> Bu this line still causes an assert "wxArgNormalizer(): format specifier
KK> doesn't match argument type" and returns crap in "path".
KK>
KK> I'm running wxWidgets 3 in x64 build.
As usual, "extraordinary claims require extraordinary evidence". If you
indeed think that something as basic as this, and used by millions of
people, doesn't work, please provide some evidence for this, e.g. a minimal
patch to the minimal wxWidgets sample showing the problem.
Regards,
VZ
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Karl Karpfen
2014-10-20 10:15:54 UTC
Permalink
Great, thanks, I'll try it!
Post by Karl Karpfen
Hi,
I thought I did understand the changes in wxString related to c_str(), but
it seems I'm wrong.
path=wxString::Format(_T("%s%dR"),m_project->m_undoPath->c_str(),m_project->m_redoSteps->size());
As far as I remember the Linux-Version of wxWidgets required the c_str()
for m_undoPath, without I got a crash. Now with 3.0 I changed this line to
path=wxString::Format(_T("%s%dR"),m_project->m_undoPath->char_str(),m_project->m_redoSteps->size());
but this results in an compiler error. So what is the correct way of
formatting a wxString this way with using an other wxString?
Thanks!
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Loading...