Why not just read your 10 megs of data directly into tmp2 using stl streams?
Seems like a waste to read 10 megs of data into a char* only to have to allocate another 10 megs by virtue of copying it to an stl string
Main Topics
Browse All TopicsI have a large character array in c++ and I need to get it into a string type variable. How is this done. I have this so far.
char *tmp1;
string tmp2;
/* read data into tmp1 */
/* tmp1 now has 1-10 megs of data in it */
temp2 = tmp2.insert(0, tmp1);
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
FYI:
temp2 = tmp2.insert(0, tmp1);
The above code will insert into the first position of tmp2, the tmp1 string, until it reaches a NULL terminator.
If this is not working for you, I must assume tmp1 must have NULL terminators within the middle of the data.
In that case, you need to determine the end point of your data, so you can pass it to your std::string.
Then the following should work:
tmp2.insert(0, tmp1, tmp1 + SizeOfDataInTmp1);
or
tmp2.assign(tmp1, tmp1 + SizeOfDataInTmp1);
Here's an example of how to read stuff from a file right into a string:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
void main()
{
std::ifstream in;
in.open("C:\\test.txt");
if(in.good())
{
string buffer;
std::ifstream::_Iter beginÍter(in);
std::ifstream::_Iter endIter;
std::copy(beginÍter, endIter, back_inserter(buffer));
cout << buffer << endl;
in.close();
}
}
>>std::copy(beginÍter, endIter, back_inserter(buffer));
For that method to work std::string would need to have a push_back member function.
But std::string does not.
You could use vector<char> with that method, but that would not give you an std::string object.
string file_contents;
copy(istream_iterator<char
>>http://msdn.microsoft.co
That's a link for VC++ .Net
http://gcc.gnu.org/onlined
That's a link for GNU compiler.
The C++ standard does not have a push_back function for the std::string class with in the basic_string section (21.3 of the ISO/IEC 14882:1998(E) C++ standard)
However, in reading some Newsgroup topics on this, it's seems that the standard does support push_back on std::string indirectly. And that this support was added late to the standard.
VC++6.0 does not support this, and I'm sure you'll find other compilers that will not support the push_back member function for std::string.
However, IAW the C++ standard, it would seem your code is correct.
Business Accounts
Answer for Membership
by: jkrPosted on 2004-12-20 at 09:30:25ID: 12868584
You'd just use
tmp2 = tmp1;
to assign a 'char*' to a std::string. Alternatively,
tmp2.assign(tmp1);
would also work.