null-terminated string to std::string 
Author Message
 null-terminated string to std::string

Hi,

Is it possible to load a null-terminated string directly into a string
object, instead of
char s[256];
std::string t = s;

--

Olaf van der Spek
Almere, Holland

http://www.*-*-*.com/



Wed, 14 Nov 2001 03:00:00 GMT  
 null-terminated string to std::string

Quote:
> Hi,

Hi.

Quote:
> Is it possible to load a null-terminated string directly into a string
> object, instead of
> char s[256];
> std::string t = s;

This is perfectly legal:
    std::string t("Hi, I'm a string named 't'.");

--
--
Larry Brasfield
Above opinions may be mine alone.



Wed, 14 Nov 2001 03:00:00 GMT  
 null-terminated string to std::string
You mean like:
    std::string s = "Can I do this?" ;

Yes.

HTH

-- Aaron [MVP]
---------------------
Aaron J Margosis


Quote:
> Hi,

> Is it possible to load a null-terminated string directly into a string
> object, instead of
> char s[256];
> std::string t = s;

> --

> Olaf van der Spek
> Almere, Holland

> http://www.euronet.nl/users/spek/



Thu, 15 Nov 2001 03:00:00 GMT  
 null-terminated string to std::string

Larry Brasfield heeft geschreven in bericht ...

Quote:
>> Hi,
>Hi.

>> Is it possible to load a null-terminated string directly into a string
>> object, instead of
>> char s[256];
>> std::string t = s;

>This is perfectly legal:
>    std::string t("Hi, I'm a string named 't'.");

No, sorry, I meant something like this.
std::string s;
a(s.c_str());

void a(char* p)
{
    strcpy(p, "This can be done");

Quote:
}

>--
>--
>Larry Brasfield
>Above opinions may be mine alone.




Thu, 15 Nov 2001 03:00:00 GMT  
 null-terminated string to std::string

Quote:

> Larry Brasfield heeft geschreven in bericht ...


...
> >> Is it possible to load a null-terminated string directly into a string
> >> object, instead of
> >> char s[256];
> >> std::string t = s;

> >This is perfectly legal:
> >    std::string t("Hi, I'm a string named 't'.");

> No, sorry, I meant something like this.
> std::string s;
> a(s.c_str());

> void a(char* p)
> {
>     strcpy(p, "This can be done");
> }

There is no way to do that.  The std::string
class does not guarantee that characters
are stored contiguously, so the following
will not work reliably, either:
void TrompUponString(std::string & s)
{
    strcpy(&s[0], "stuff that may trash who knows what");

Quote:
}

It will compile, and it might work on some
implementations (with a suitable check for
string length), but it could fail for a perfectly
valid implementation of std::string.

--
--
Larry Brasfield
Above opinions may be mine alone.



Thu, 15 Nov 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Assignind a NULL C string to an std::string

2. convert non std::string to std::string

3. Any Command to save an Null-Terminated String to a File

4. stupid question about null terminated strings

5. NULL terminated strings

6. Arrays of strings (null terminated pointers)

7. How to read a non NULL terminated string from a file

8. Null-terminated C strings

9. Null-terminated C strings

10. Concatenating null-terminated strings

11. array of null terminates strings

12. Null Terminated String from VBA to C DLL

 

 
Powered by phpBB® Forum Software