473,750 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C Style Strings

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
Jun 1 '06 #1
89 5141

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)+strl en(s2)+1];
strcpy(result,s 1);
strcat(result,s 2);

Regards
Jiri Palecek

Jun 1 '06 #2
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
Jun 1 '06 #3
On Thu, 01 Jun 2006 08:35:36 +0100, scroopy <sc*****@nospam .com>
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
Jun 1 '06 #4
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,s tr1);
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::strin g(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

Jun 1 '06 #5
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,s tr1);
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

Jun 1 '06 #6
"kwikius" <an**@servocomm .freeserve.co.u k> 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
Jun 1 '06 #7
Richard Bos said:
"kwikius" <an**@servocomm .freeserve.co.u k> 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)
Jun 1 '06 #8
Richard Bos wrote:
"kwikius" <an**@servocomm .freeserve.co.u k> 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

Jun 1 '06 #9
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

Jun 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8322
by: Christopher Benson-Manica | last post by:
I have a C-style string (null-terminated) that consists of items in one of the following formats: 14 characters 5 characters space 8 characters 6 characters colon 8 characters 5 characters colon 8 characters Items are delimited by semicolons or commas. I have to produce a string delimited only by semicolons and containing items in the first two formats only. For example,
5
2427
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My problem is in comparing the customer number I just read in with those in the list. I'm not sure how to go about comparing c-style strings for greater than, less than.. here is how I am currently trying to do it: while( ( custinfo.number >...
3
2119
by: joealey2003 | last post by:
Hi all... I included a css file on my html and i need to check some properties. The html is: <style id="myid" src="mycsspage.css"> </style> Now i need something to access it like: alert(document.getElementById("myid").bottomline.color);
7
1951
by: seans | last post by:
Hi, I need to change the font, font size, and color of a button on a form. I need to save the current style settings and restore them later. Is there an easy way of doing that? Sorry if there is a simple solution to this; I am still learning Javascript. Thanks again.
5
10227
by: Chuck Bowling | last post by:
Maybe I'm doing something wrong or just don't understand the concept, but i'm having a problem with default properties. My impression of how a default property should act is this; MyClass c = new MyClass(); c = "my text"; In the line above, the string is assigned to a field in the class instance.
2
1417
by: Xiaoshen Li | last post by:
Dear All, I hear a lot that cstring cannot be changed. But my code: /************************************************************* * Testing cstring(a pointer) * **********************************************************/ #include <iostream> using namespace std;
4
2011
by: Kza | last post by:
Hi, just in the process of maintaining some software that used some funy old string library and char*s , and we are updating everything to use std::strings. (or should I say std::basic_string<>s) I find it wierd that that all the new c++ ansi style librarys like the streams and file handling classes still expect us to use old style char* type strings. For example, ofstreams open function expects the filename as a char* parameter rather...
2
2226
by: Kamjah Kogumah | last post by:
A Dev Opera article suggest using code 2 instead of code 1 in performance-critical functions. code 1: a += 'x' + 'y'; code 2: a += 'x'; a += 'y';
16
3671
by: yu_kuo | last post by:
Is there any comparison data on perfomance difference between std::string and c style string? Or maybe if there are source code which could be used to measuer on different compiler/platform, in a systematic way?
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.