handle \x0 character in a string

T

thobiasvakayil

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
 
J

James Kanze

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 ) ;
 
T

thobiasvakayil

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:[email protected]
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.
 
J

James Kanze

On Dec 17, 7:11 am, (e-mail address removed) 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.
 
N

Noah Roberts

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.
 
J

James Kanze

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.)
 
S

SG

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top