Combining std::string array into a CHAR*

Is there any way to combine an array of strings into one single CHAR* (not an array)?

Thanks :)
Sum the string together and call c_str on the final object ( you need to copy that final sequence if you want to keep it )
closed account (3pj6b7Xj)
To put it in more technical terms, allocate a buffer large enough to hold the data, then read the array index by index, use the length of each entry to determine the write offset in the buffer.......if the first string is "hello world" which is 12 characters long, you know the next write offset will be that +1 you may want to to store this offset in some variable to use it as your program reads your strings.

you can also combine an array of strings the following way.

string house("my house");
string dog("a big dog");
string car("fast car");

combine them all together.

string combine;
combine.append(house);
combine.append(dog);
combine.append(car);

new string value is now --> "my housea big dogfast car"

closed account (3pj6b7Xj)
oops wait, you said array of strings right? of course.

vector<string>Text;

Text.push_back("rock and roll");
Text.push_back("airplanes can fly");
Text.push_back("tires are good.");

combine them together...

string combine;

for(int i=Text.size();i>0;i--)
combine.append(Text[i]);

result is ---> "rock and rollairplanes can flytires are good."

of course, this is a vector array which is sort of like the same thing as an array of strings.

string mystringarray[10]; /// <---- but now its fixed, if you use a vector the array can grow and decrease in size dynamically during runtime.
Thanks guys, but now I have a new problem:

I have a string:
string saHack [10000];

And I use getline() to get each line of a file into each element of that array.

Now I'm trying to combine them all into one string variable:

string sHack = "";

But when I try to use the append function it only appends the first element of the saHack array:

1
2
3
4
5
for (int i = 0;;i++)
{
       if (saHack [i] == "DONE") break; // I use << endl << "DONE"; after I'm done writing to the file, this is just a check to see if we have reached the end of the string array.
       sHack.append (saHack [i]);
}


Like I said before sHack only has the first element of saHack, and not all of it.

~~~~~NOTE:~~~~~

The text "DONE" is in element 479, not 1, so "DONE" is not the reason why it stops appending, if that is what some of you are thinking. I found this out by running this code:

1
2
3
4
5
int i;
for (i = 0;;i++) if (saHack [i] == "DONE") break;
char ca [50];
sprintf (ca, "%i", i);
MessageBox (NULL, ca, "", MB_OK);


The message box prints "479".

Thanks for your help guys :D
Last edited on
I would try printing out each of the elements of saHack as well to make sure they aren't empty, or some other strange value.
I tried using

MessageBox (NULL, saHack [0].c_str (), "", MB_OK);

And other elements of the array and they all have each line of the file (depending on what element I tried accessing)

My main goal of all of this is to get array of strings into a single string. So if I had:

1
2
3
string saHack [] = {
     "Hello", "World", "This", "Is", "Text!"
};


I could use the entire array in SetWindowText:

 
SetWindowText (GetDlgItem (hWnd, CODE_ARCHIVE_CODE), saHack);


But that doesn't work (neither does saHack.c_str ()) so I'm trying to converting everything into one string (sHack) and see if I can:

 
SetWindowText (GetDlgItem (hWnd, CODE_ARCHIVE_CODE), sHack.c_str ());




So my overall problem is not being able to use saHack[] in SetWindowText :P, maybe I should of said that at the beginning xD
Last edited on
NEVER MIND ON ALL OF THIS.... I got it to work :D
Topic archived. No new replies allowed.