O.K. Take it easy, don't break anything. Just breath slowly!
So, I have this problem since I started this project and now I think I know what happens (but not how to solve it).
I have some string saved in a file like this "setting1=value1;setting2=value2;setting3=value3". I get this string into a char* variable then I have to split it and read each of them like setting1=value1 then setting2=value2 then setting3=value3. What happens is that depending of the stored values the program just crashes like a piece of junk.
My code is something like this
1 2 3 4 5 6 7 8 9 10 11
|
char* pure_string = getfromfile("data.txt"); //It gets the char* variable correctly, I tested it with a MessageBox
vector<char*> settings_vector; //This is the vector that is supposed to hold each pair of string (setting1=value1, setting2=value2, setting3=value3)
char* string_split = strtok(pure_string, ";"); //Let's start splitting by ";" firstly
while (string_split != NULL) {
settings_vector.push_back(string_split); //Try to add the value at the end of the vector
MessageBox(NULL, string_split , "Checking", MB_OK|MB_ICONINFORMATION); //Show the current splitted value
string_split = strtok (NULL, ";"); //Keep splitting
}
|
After splitting each pair of (setting=value) like string we take each value stored into the vector and split it by "=" to get the value but it doesn't get to that because an unexpected error occurs, specially if the value contains spaces.
So, if I have the original string stored into the data.txt file like "setting1=12345;setting2=this is my name;setting3=true" when the second pair "setting2=this is my name" is being stored into the settings_vector the application crashes. If I remove the spaces it works O.K. again.
I just don't understand why is it so difficult to work with char arrays and vectors in C++. I feel like I miss some important notion about this. Help me, thanks!