473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Inserting a char into a string.

Hey,
Sorry if this is a really stupid question - I'm new to C++.

I have a program like this:

std::string str;
....
....
....
char buffer[4];

The variable buffer has been filled with four characters.
How do I now make str equal to buffer?
ie I have "ABCD" in buffer and I would like "ABCD" in str.

Thanks
Connell
Jul 23 '05 #1
2 1427
"Connell Gauld" <co*****@freebreakfast.co.uk> wrote in message
news:cu**********@news8.svr.pol.co.uk...
Hey,
Sorry if this is a really stupid question - I'm new to C++.

I have a program like this:

std::string str;
...
...
...
char buffer[4];

The variable buffer has been filled with four characters.
How do I now make str equal to buffer?
ie I have "ABCD" in buffer and I would like "ABCD" in str.


str.assign( buffer, buffer+4 );
or
str.assign( buffer, 4 );

(or pass the same two parameters defining a range to
the constructor of std::string).

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 23 '05 #2
assigning works as well:
string s;
char c[5];
memset(&c, '\0', sizeof(c));
strncpy(c, "ABCD", 4);

s = c;

cout << c << endl;
cout << s << endl;

Jul 23 '05 #3

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

Similar topics

1
by: Kay | last post by:
There are two different function. The first one loads data from a txt file and adds the name and num to the linked list. It can display the context of the linked list. I'm designing to use second...
4
by: epaetz | last post by:
I'm doing a bcp out of a table to a file. Some of the fields in a record may have an empty string. When I bcp out to the file and examine it, the fields that have an empty string in the database...
3
by: Luminal | last post by:
greetings i'm developing an C# application using Access as database and I'm having problems inserting data containing the ' char and the " char. What is the best practice to insert this chars on...
15
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract...
4
by: irsmartt | last post by:
How can I allow a user (in a command prompt) to enter a string of characters and put spaces in it? I use Dev C++. Example: char google; cin>> google; Whenever I try this and output the...
15
by: Jaraba | last post by:
I am working in a project that I need to parse an arrayt an select records based upon the values parsed. I used the functions developed by Knut Stolze in his article 'Parsing Strings'. I am...
3
by: rcoco | last post by:
Hi, I want to share this problem. I have a datagrid that will help me Insert data into sql database. So I made a button On my form so that when I press the button a new row on datagrid should be...
2
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000...
5
by: dos360 | last post by:
Hello, I have two tables, one is a list of activities, the other a list of participants. I want to insert one record in the activities table and then using its identity column as foreign key, I...
0
by: Edwin.Madari | last post by:
-----Original Message----- statement prepared first and executed many times with exectemany - db API http://www.python.org/dev/peps/pep-0249/ inline statemets can be exeucuted only. hope that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.