Simple C++ string array question

OneOfTheseDays

Diamond Member
Jan 15, 2000
7,052
0
0
Ok right now I'm building a string character by character by reading in one byte of memory at a time until I hit a null character. There is a maximum length for the string (50), and the first thing I do is to null out the entire char array so that every spot in the array is now NULL. I then read in character by character from memory into this array and stop when i read in a null character.

Ok, now my problem is I need to be able to do this many times over, and I need to be able to save the string I created into memory. The project I'm working on needs to extract each string from argv[] in memory, add a separate filename as argv[0] and append the rest of argv afterwards. So basically I'm pulling out argv[] and the filename from memory and creating a new array with argv[0] being the filename, and the rest argv[1...argc] being the argv strings themselves. I need to be able to save off each string, and we know how many strings there are because argc tells us how many there are. How can I do this so that each string I pull out is saved into memory so that I can make a new array containing all of these strings?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
create a vector for the type string

vector <string> m_aString;

string strTemp;

.. build the strTemp from your memory array


m_aString.pushback(strTemp);


Also, consider using a memcpy to pull 50 bytes from memory into a tempory character buffer.

Then take a string object and set it equal to the the temp memory area.

char tempBuffer[51] = {51*0x00};
memcpy (tempBuffer, memory location);
string strtemp = tempBuffer;