Discussion:
dealing with backslash within a quoted string
e***@gmail.com
2014-10-04 23:33:42 UTC
Permalink
I'm updating an application and cannot remember how to escape the "\"
character in strings. Previously I used MSVS2010 and I am now using
MSVS2012. With 2010, when I drop one or more files on my application I get
an array of one or more names which looked vaguely like:

"H:\Music\Test.wav"

with 2012 the same file drop name would look like:

"H:\\Music\\Test.wav"

note that in the 2012 version of the file name which is passed from the
operating system we now have 2 backslashes. However, when I pass this
string to the file opening function I get an Assert & Exception. If I pass
the original string, everything works just fine. It would not surprise me
if this is a bug in the very old library I'm forced to use to open these
files. What I need to know is how to escape the backslashes so that I can
do something that looks like:

wxString name = wxT("H:\\Music\\Test.wav "); size_t done =
name.Replace(wxT("\\"), wxT("\")); I tried:

size_t done = name.Replace(wxT("\\\\"), wxT("\\")); but, while it compiled,
it failed to do the job.
--
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
'Ray Satiro' via wx-users
2014-10-05 01:58:44 UTC
Permalink
What I need to know is how to escape the backslashes so that I can do
wxString name = wxT("H:\\Music\\Test.wav "); size_t done =
size_t done = name.Replace(wxT("\\\\"), wxT("\\")); but, while it
compiled, it failed to do the job.
Those replaces look all wrong. Review
http://en.wikipedia.org/wiki/Escape_sequences_in_C
When you have a string literal like "H:\\Music\\Test.wav" what is
actually stored in memory is "H:\Music\Test.wav".
Try this
wxMessageBox("H:\\Music\\Test.wav");
You will see H:\Music\Test.wav. No replacement is required.

If you are working with wxString and passing it to other libraries you
should review the documentation particularly 'traps for the unwary':
http://docs.wxwidgets.org/trunk/classwx_string.html#string_gotchas

Another thing, although minor since fopen and wxWidgets file functions
will still work, is you have a space after the filename. That could
possibly cause a problem with your 3rd party file open function.
--
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
Shawn H Corey
2014-10-05 12:31:47 UTC
Permalink
On Sat, 04 Oct 2014 21:58:44 -0400
Post by 'Ray Satiro' via wx-users
Those replaces look all wrong. Review
http://en.wikipedia.org/wiki/Escape_sequences_in_C
When you have a string literal like "H:\\Music\\Test.wav" what is
actually stored in memory is "H:\Music\Test.wav".
Try this
wxMessageBox("H:\\Music\\Test.wav");
You will see H:\Music\Test.wav. No replacement is required.
True. You only have to double the backslash inside literals. If you
read the file name from a file, don't double the backslash in the file.
--
Don't stop where the ink does.
Shawn
--
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...