[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

handle \x0 character in a string

thobiasvakayil

12/17/2008 6:11:00 AM

I have a problem like this.
How to handle \x0 character inside a string.
Eg:-

#include <string>
#include <iostream>
int main()
{
string str("abcdef\x0 xyz");

cout << "length =" << str.length() << endl;
cout << "string=" << str << endl;
return 0;
}

The output of this program is as follows :
length =6
string=abcdef


But I have to get the output as :
abcdef xyz

Can somebody help ?
Thanks in advance

Regards,
Thobias
6 Answers

James Kanze

12/17/2008 9:18:00 AM

0

On Dec 17, 7:11 am, thobiasvaka...@gmail.com wrote:
> I have a problem like this.
> How to handle \x0 character inside a string.
> Eg:-
>
> #include <string>
> #include <iostream>
> int main()
> {
> string str("abcdef\x0 xyz");

> cout << "length =" << str.length() << endl;
> cout << "string=" << str << endl;
> return 0;
> }

> The output of this program is as follows :
> length =6
> string=abcdef

> But I have to get the output as :
> abcdef xyz

Well, you have to count the characters, but:

string str( "abcdef\0 xyz", 11 ) ;

should work. Or if you don't want to count them:

static char const strInit[] = "abcdef\0 xyz" ;
string str( strInit, sizeof( strInit ) - 1 ) ;

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

thobiasvakayil

12/17/2008 11:15:00 AM

0

On Dec 17, 2:17 pm, James Kanze <james.ka...@gmail.com> wrote:
> On Dec 17, 7:11 am, thobiasvaka...@gmail.com wrote:
>
>
>
>
>
> > I have a problem like this.
> > How to handle \x0 character inside a string.
> > Eg:-
>
> > #include <string>
> > #include <iostream>
> > int main()
> > {
> >     string str("abcdef\x0 xyz");
> >     cout << "length =" << str.length() << endl;
> >     cout << "string=" << str << endl;
> >     return 0;
> > }
> > The output of this program is as follows :
> > length =6
> > string=abcdef
> > But I have to get the output as :
> > abcdef xyz
>
> Well, you have to count the characters, but:
>
>     string str( "abcdef\0 xyz", 11 ) ;
>
> should work.  Or if you don't want to count them:
>
>     static char const   strInit[] = "abcdef\0 xyz" ;
>     string str( strInit, sizeof( strInit ) - 1 ) ;
>
> --
> James Kanze (GABI Software)             email:james.ka...@gmail.com
> Conseils en informatique orientée objet/
>                    Beratung in objektorientierter Datenverarbeitung
> 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34- Hide quoted text -
>
> - Show quoted text -

Thanks for your support. It's working fine.
But actually my input data is of type string.
So I can't use the first case.
In the second case, I can't able to convert the string to a const char
with \0.

James Kanze

12/17/2008 1:30:00 PM

0

On Dec 17, 12:14 pm, thobiasvaka...@gmail.com wrote:
> On Dec 17, 2:17 pm, James Kanze <james.ka...@gmail.com> wrote:
> > On Dec 17, 7:11 am, thobiasvaka...@gmail.com wrote:

> > > I have a problem like this.
> > > How to handle \x0 character inside a string.
> > > Eg:-

> > > #include <string>
> > > #include <iostream>
> > > int main()
> > > {
> > > string str("abcdef\x0 xyz");
> > > cout << "length =" << str.length() << endl;
> > > cout << "string=" << str << endl;
> > > return 0;
> > > }
> > > The output of this program is as follows :
> > > length =6
> > > string=abcdef
> > > But I have to get the output as :
> > > abcdef xyz

> > Well, you have to count the characters, but:

> > string str( "abcdef\0 xyz", 11 ) ;

> > should work. Or if you don't want to count them:

> > static char const strInit[] = "abcdef\0 xyz" ;
> > string str( strInit, sizeof( strInit ) - 1 ) ;

> Thanks for your support. It's working fine. But actually my
> input data is of type string. So I can't use the first case.
> In the second case, I can't able to convert the string to a
> const char with \0.

If you're input data is of type std::string, then there's no
problem; it will already contain whatever it's supposed to
contain. And I don't understand the second sentence; you
obviously can't convert a string to a char const, since a string
contains more than one character. And that has nothing to do
with my second example.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Noah Roberts

12/17/2008 4:40:00 PM

0

thobiasvakayil@gmail.com wrote:
> I have a problem like this.
> How to handle \x0 character inside a string.
> Eg:-
>
> #include <string>
> #include <iostream>
> int main()
> {
> string str("abcdef\x0 xyz");
>
> cout << "length =" << str.length() << endl;
> cout << "string=" << str << endl;
> return 0;
> }
>
> The output of this program is as follows :
> length =6
> string=abcdef

That's because character strings (char*, which "" is) terminate with \0.
The string constructor that accepts char* assumes that it's a null
terminated stream and acts accordingly; it stops grabbing characters
when it finds a \0.

James Kanze

12/18/2008 9:17:00 AM

0

On Dec 17, 5:39 pm, Noah Roberts <n...@nowhere.com> wrote:
> thobiasvaka...@gmail.com wrote:
> > I have a problem like this. How to handle \x0 character
> > inside a string.
> > Eg:-

> > #include <string>
> > #include <iostream>
> > int main()
> > {
> > string str("abcdef\x0 xyz");
>
> > cout << "length =" << str.length() << endl;
> > cout << "string=" << str << endl;
> > return 0;
> > }

> > The output of this program is as follows :
> > length =6
> > string=abcdef

> That's because character strings (char*, which "" is)
> terminate with \0.

Strings, in C++, aren't terminated with a '\0'. String literals
(such as "") are, but their type is char const[], not char*.
In the code in question, "abcdef\x0 xyz" is a string literal
with two '\0', and type char const[12].

> The string constructor that accepts char* assumes that it's a
> null terminated stream and acts accordingly; it stops grabbing
> characters when it finds a \0.

The constuctor accepts char const*, not char*. And it's an
interesting question: why isn't there a template constructor:

template< size_t l >
string( char (&literal)[ l ] ) ;

? That would work here. (I've not verified the rules---maybe
it would be ambiguous with string::string( char const* ). Which
would still be needed in order to interface with C.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

SG

12/18/2008 12:21:00 PM

0

On 18 Dez., 10:17, James Kanze <james.ka...@gmail.com> wrote:
> The constuctor accepts char const*, not char*. And it's an
> interesting question: why isn't there a template constructor:
>
> template< size_t l >
> string( char (&literal)[ l ] ) ;
>
> ? That would work here. (I've not verified the rules---maybe
> it would be ambiguous with string::string( char const* ). Which
> would still be needed in order to interface with C.)

I just checked it out of curiosity. In the presence of this templated
constructor

string("hello")

would still invoke the constructor taking const char* as argument.
Apparently array-to-pointer decay is too little of a "conversion" to
make the compiler select a templated function over a non-templated
function. But if you convert the string(const char*) constructor to a
templated one with SFINAE a la

template<typename T>
string(const T*, typename enable_if_same<T,char,char>::type
dummy=0);

Then you get the desired behaviour.

Cheers!
SG