- Escape character treatment in string library functions
- Posted by rejithomas.d@gmail.com on January 16th, 2008
Can I change the escape character used by string libraries?. My
requirement is to
parse a string in format "domain\username" and change it into domain\
\username.
The C library functions takes \ as an escape character and also treat
'\r' etc special making it difficult to parse even char by char.
Can anyone suggest any solution to this issue
- Posted by ekkehard.horner on January 16th, 2008
rejithomas.d@gmail.com schrieb:
http://www.cppreference.com/escape_sequences.html
- Posted by Ben Bacarisse on January 16th, 2008
rejithomas.d@gmail.com writes:
You'd have to say which libraries (and possibly which functions in
them). Most of the C libraries I've used don't treat \ in any special
way. C itself does, but that is not a problem since you write the
strings so you can just write what C requires. [I'm talking C because
you do later on, but most languages have an escape mechanism for
string literals and so on.]
You can do that just be looking for the \ and adding a \ in front.
You may need to scan twice: once to count the \s and once to copy the
string to newly allocated storage, adding the extra \s on the way.
This suggests you have not characterised your problem. C treats \ as
an escape character, but very few libraries do and, as far as I can
recall, non of the standard C library functions do. Even when a
library does, why does it make it hard to parse "char by char"? An
example might help illustrate what your problem is. You may well be
confusing what the language does with what the library does.
--
Ben.
- Posted by Richard Heathfield on January 16th, 2008
rejithomas.d@gmail.com said:
In C, the escape character only affects string literals, i.e. strings
embedded within your program, and the occasional character literal. The
solution there is to use \\ rather than \ to get the literal backslash
character. For example:
char uname[] = "domain\\username";
But if you're dealing with data captured at runtime, e.g. from a file or
from an API call or from the user, you don't need to worry about it. You
only need to "escape the backslash" in string literals and the '\\'
character literal.
So, for example, if you do this:
#include <stdio.h>
int main(int argc, char **argv)
{
if(argc > 1)
{
puts(argv[1]);
}
return 0;
}
and run it with a command line argument of domain\username then you will
get the output:
domain\username
which is exactly what you'd hope and expect.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
- Posted by Malcolm McLean on January 16th, 2008
<rejithomas.d@gmail.com> wrote in message
- Posted by Malcolm McLean on January 16th, 2008
<rejithomas.d@gmail.com> wrote in message
news:83c99c40-9b92-441a-ba95-f6536ed4759c@p69g2000hsa.googlegroups.com...
{
size_t count = 0;
size_t i = 0;
size_t j = 0;
char *answer = 0;
for(i=0;in[i];i++)
if(in[i] == '\\')
count++;
answer = malloc( strlen(in) + count + 1);
if(!answer)
return 0;
for(i=0;in[i];i++)
{
answer[j++] = in[i];
if(in[i] == '\\')
answer[j++] = '\\';
}
answer[j] = 0;
return answer;
}
int main(int argc, char **argv)
{
int i;
for(i=0;i<32;i++)
printf("%s\n", doublebackslashes("My\\Fred"));
return 0;
}
I've knocked up a little funcion for you.
I suspect that what you really need is a "make C escapes" however, which is
a little more work.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
- Posted by rejithomas.d@gmail.com on January 17th, 2008
On Jan 16, 8:03 pm, "Malcolm McLean" <regniz...@btinternet.com> wrote:
Thanks everyone for the reply. But the issue I am facing is
I get a string literal in the format "domain\username" (with a single
\ and not \\ ,. say char *str= "dom\ret") .If I try to parse the
string char by char C treats '\r' as a single character.
For eg:
char *str="dom\ret";
while( *(str++) !='\0')
if(*str == '\\' )
printf("found");
is not working since it sees '\r' as a single char .
Thanks
Reji
- Posted by Richard Heathfield on January 17th, 2008
rejithomas.d@gmail.com said:
The source code is broken. '\r' /is/ a single char. If you mean a literal
slash followed by a literal r, then you need:
char *str="dom\\ret";
or, better:
const char *str="dom\\ret";
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
- Posted by Logan Shaw on January 17th, 2008
rejithomas.d@gmail.com wrote:
It isn't accurate to say that when you are doing things character
by character that C is treating "\r" as a single character. There
are two reasons why this isn't accurate:
(1) When you are doing the character by character processing,
there is no C. There is only a binary. The binary was
produced by a C compiler, but that is immaterial at that
point.
(2) When you are doing the character by character processing,
there is no sequence of characters "\" followed by "r".
There is only a carriage return, which is a single character.
Nothing is treating anything as a single character. There
is no special processing going on. There just is a single
character.
If you don't want C to produce a string constant with a
carriage return in it, then don't tell the C compiler to
produce a string constant with a carriage return in it.
If you want a string constant with a "\" character followed
by a "r", then write this: "\\r". Then the "\\" will be
translated into a "\" when the source code is turned into
a string constant.
- Logan
- Posted by CBFalconer on January 17th, 2008
Richard Heathfield wrote:
The OP should try using '/' instead. Even if he is using that
confused system, Winders, I think he will find that the system
routines interpret the '/' separators correctly. Not 100% certain
of this, though.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com
- Posted by Jim Langston on January 26th, 2008
rejithomas.d@gmail.com wrote:
If the string is being read by an external source, and has the characters
'\' and 'r' next to each other, they will be interpreted as two different
characters, '\' and 'r'. If, however, "\r" is typed in the source code, it
will be treated as a single escaped character. The way to prevent this is
in your source code you escape the \ with "\\". I.E.
char* str = "dom\\ret";
will produce '\' and 'r' as 2 seperate characters. Reading the text from a
file with "dom\ret" in it will also produce 2 seperate characters. The only
real concern you should have is always escape your \ in your source code.
Now, there are some exceptions with external libraries. There are, for
example, RE libraries that would treat the string "dom\ret" as the r being
escaped. In which case you need to manually escape the \ in the string.
In your example given, this will work:
char* str="dom\\ret";
while( *(str++) !='\0')
if(*str == '\\' )
printf("found");
should produce the output
found
--
Jim Langston
tazmaster@rocketmail.com
- Posted by saroj.slg@gmail.com on February 8th, 2008
you can use the additional \ with the \ in "domain\username"
like "domain\\username".
this will print domain\username instead of domainsername......
hope this will help....
thanks.