Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Escaping backslash char in string

Status
Not open for further replies.

BTon15

Programmer
Nov 21, 2001
29
0
0
GB
Hopefully this is really simple for somebody out there:

I want to build a string that I'm going to pass to the system() function command to do a Unix sed. I want my string to look like this:

-e '/\[SECTION\]/d'

i.e. I want my sed to remove a line that contains [SECTION]
The sed works fine, but I'm having problems creating the string in C. I'm doing this as follows:

strcpy (str, " -e '/\\\[SECTION\\\]/d' ");

My compiler is giving me this:

"tmp.c", line 9.22: 1506-235 (W) Incorrect escape sequence \[. \ ignored.
"tmp.c", line 9.22: 1506-235 (W) Incorrect escape sequence \]. \ ignored.

The string does seem to end up with what I want though, despite the warnings. I've tried removing some of the backslashes but this makes it worse.

Any ideas how I can compile this to avoid the warnings, whilst still ending up with the string I want?

Many thanks in advance.
 
Use \\ instead of \\\.
\\ = "\", then you have \[ which isn't a valid escape sequence.
 
This works for me...
Code:
strcpy (str, " -e '/\\[SECTION\\]/d' ");
Hope this helps.
 
Thanks guys - that works great.
I was sure I had already tried that before, but maybe I couldn't see the wood for the trees!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top