Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C Style Strings

Reply
Thread Tools

C Style Strings

 
 
scroopy
Guest
Posts: n/a
 
      06-01-2006
Hi,

I've always used std::string but I'm having to use a 3rd party library
that returns const char*s. Given:

char* pString1 = "Blah ";
const char* pString2 = "Blah Blah";

How do I append the contents of pString2 to pString? (giving "Blah
Blah Blah")

I looked at strcat but that crashes with an unhandled exception.

Thanks
 
Reply With Quote
 
 
 
 
jpalecek@web.de
Guest
Posts: n/a
 
      06-01-2006

scroopy wrote:
> Hi,
>
> I've always used std::string but I'm having to use a 3rd party library
> that returns const char*s. Given:
>
> char* pString1 = "Blah ";
> const char* pString2 = "Blah Blah";
>
> How do I append the contents of pString2 to pString? (giving "Blah
> Blah Blah")
>
> I looked at strcat but that crashes with an unhandled exception.


That's easy, do it the C way:

char* result=new char[strlen(s1)+strlen(s2)+1];
strcpy(result,s1);
strcat(result,s2);

Regards
Jiri Palecek

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-01-2006
scroopy wrote:

> Hi,
>
> I've always used std::string but I'm having to use a 3rd party library
> that returns const char*s. Given:
>
> char* pString1 = "Blah ";
> const char* pString2 = "Blah Blah";
>
> How do I append the contents of pString2 to pString? (giving "Blah
> Blah Blah")


a) Keep using std::string for your own stuff. The std::string class has
methods that allow you to interact with C-style string interfaces of
libraries.

b) Note that in your example above, pString1 is initialized to a const
string. Any attempt to modify that string would be undefined behavior. The
danger lies in not declaring pString1 as a char const *.

c) You could do:

#include <string>
#include <algorithm>

char * strdup ( std::string str ) {
char * result = new char [ str.length() +1 ];
std::copy( str.begin(), str.end(), result );
result[ str.length() ] = 0;
return ( result );
}

int main ( void ) {
char * pString1 = "Blah ";
char const * pString2 = "Blah Blah";
pString1 = strdup( std::string( pString1 ).append( pString2 ) );
}


However, I would prefer to use std::string for my own stuff:

#include <string>

int main ( void ) {
std::string pString1 = "Blah "; // rhs returned by some library
std::string pString2 = "Blah Blah"; // rhs returned by some library
pString1.append( pString2 );
}


Best

Kai-Uwe Bux
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      06-01-2006
On Thu, 01 Jun 2006 08:35:36 +0100, scroopy <(E-Mail Removed)>
wrote:
>I've always used std::string but I'm having to use a 3rd party library
>that returns const char*s.


First you need to find out if you 'own' the returned string, i.e. if
you must free (maybe delete) the returned char*. Good C libraries
usually don't require the user to call free.

>Given:
>
>char* pString1 = "Blah ";
>const char* pString2 = "Blah Blah";
>
>How do I append the contents of pString2 to pString? (giving "Blah
>Blah Blah")


You can append a const char* to a std::string:

string myString = "Blah ";
const char* s = myLibFunc (...);
if (s) {
myString += s; // or myString.append(s);
}
// free (s); // if it's a bad lib

Best wishes,
Roland Pibinger
 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      06-01-2006
scroopy wrote:
> Hi,
>
> I've always used std::string but I'm having to use a 3rd party library
> that returns const char*s. Given:
>
> char* pString1 = "Blah ";
> const char* pString2 = "Blah Blah";
>
> How do I append the contents of pString2 to pString? (giving "Blah
> Blah Blah")


#include <malloc.h>
#include <cstring>

char* concat(const char * str1, const char* str2)
{
char * result = (char*) malloc(strlen( str1) + strlen (str2) + 1);
if( result != NULL){
strcpy(result,str1);
strcat(result, str2);
}
return result;
}

#include <iostream>
#include <string>

char* pString1 = "Blah ";
const char* pString2 = "Blah Blah";


int main()
{
// C-style
char* str = concat(pString1,pString2);
if(str != NULL){
std::cout << str <<'\n';
free(str);
}

// C++ style
std::string str1=std::string(pString1) + pString2;
std::cout << str1 <<'\n';
}

I'm not sure if that is the optimal C method. Its interesting to note
how much better the C++ version is though!

regards
Andy Little

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-01-2006
"kwikius" <(E-Mail Removed)> wrote:

> scroopy wrote:
> > How do I append the contents of pString2 to pString? (giving "Blah
> > Blah Blah")

>
> #include <malloc.h>
> #include <cstring>


This is not C...

> char* concat(const char * str1, const char* str2)
> {
> char * result = (char*) malloc(strlen( str1) + strlen (str2) + 1);


....and this is the wrong way to do this in C. And, really, also in C++:
use new.

So don't cross-post stuff like that. Follow-ups set.

Richard
 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      06-01-2006
Roland Pibinger wrote:

> First you need to find out if you 'own' the returned string, i.e. if
> you must free (maybe delete) the returned char*. Good C libraries
> usually don't require the user to call free.


Out of interest ... What is a good C library resource management
strategy? ... for example (say) modifying the following example

char* concat(const char * str1, const char* str2)
{
char * result = (char*) malloc(strlen( str1) + strlen (str2) + 1);
if( result != NULL){
strcpy(result,str1);
strcat(result, str2);
}
return result;
}

char* pString1 = "Blah ";
const char* pString2 = "Blah Blah";

int main()
{
char* str = concat(pString1,pString2);
if(str != NULL){
std::cout << str <<'\n';
free(str); // How to avoid this in C-style?
}
}

regards
Andy Little

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-01-2006
Richard Bos said:

> "kwikius" <(E-Mail Removed)> wrote:
>
>> scroopy wrote:
>> > How do I append the contents of pString2 to pString? (giving "Blah
>> > Blah Blah")

>>
>> #include <malloc.h>
>> #include <cstring>

>
> This is not C...


....and it's not C++ either, so I'm not sure why you set followups to clc++.

The entire article, in fact (his, not yours), was a classic example of the
kind of thing you get in the Hackitt and Scarper school of programming.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      06-01-2006
Richard Bos wrote:
> "kwikius" <(E-Mail Removed)> wrote:
>
> > scroopy wrote:
> > > How do I append the contents of pString2 to pString? (giving "Blah
> > > Blah Blah")

> >
> > #include <malloc.h>
> > #include <cstring>

>
> This is not C...
>
> > char* concat(const char * str1, const char* str2)
> > {
> > char * result = (char*) malloc(strlen( str1) + strlen (str2) + 1);

>
> ...and this is the wrong way to do this in C.


Out of interest what is the right way? BTW... It would seem to me to
make sense to cross-post this to comp.lang.c as it is somewhat O.T. for
C++. I think It makes sense in terms of a language comparison though,
don't you think?

> And, really, also in C++:


hmmm... Touchy arent we? I showed a C++ way in my previous post.

> use new.


Sure? .. The best way is not to allocate resources the users has to
manage , isnt it?.

> So don't cross-post stuff like that. Follow-ups set.


Hmm... Are you sure I wouldnt get a better answer to the how to do it
in C question in a C newsgroup?

regards
Andy Little

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      06-01-2006
Richard Heathfield wrote:
> Richard Bos said:
>
> > "kwikius" wrote:
> >
> >> scroopy wrote:
> >> > How do I append the contents of pString2 to pString? (giving "Blah
> >> > Blah Blah")
> >>
> >> #include <malloc.h>
> >> #include <cstring>

> >
> > This is not C...

>
> ...and it's not C++ either, so I'm not sure why you set followups to clc++.
>
> The entire article, in fact (his, not yours), was a classic example of the
> kind of thing you get in the Hackitt and Scarper school of programming.


Thats great! Thanks! Its always good to get positive feedback!

regards
Andy Little

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Why do so many new style ansi streams and files etc, still use old style strings? Kza C++ 4 03-03-2006 07:00 PM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments