Going crazy with adding char* values to a vector

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!
Last edited on
...why aren't you using std::strings?

Also, I don't have access to a compiler ATM, so what exactly is the error you're getting when it crashes?
Why is the first parameter to strtok NULL on line 10?

-Albatross
Last edited on
What is this line? settings_vector.push_back(settings_array); What is settings_vector and settings_array?

Sorry Peter87, settings_vector is my vector, settings_array is in fact string_split. What a mess in my head.
Albatross, NULL is a null pointer, which means that strtok must keep scanning the same variable to the end
man wrote:
char *strtok(char *str, const char *delim);
On the first call to strtok() the string to be parsed should be specified in str. In each subsequent call that should parse the same string, str should be NULL.


Fix the names of your variables. ¿where is the error ocurring?
The error occurs when trying to push the value resulted from splitting the hole pure_string into the settings_vector (line 8).

Everything crashes at run-time when the value to be stored contains spaces (and maybe other non alphanumeric characters). It doesn't return any error. It's like spaces cannot be stored inside the char* elements of a vector, or maybe it's a pointer issue.
Last edited on
It's very hard to guess what is wrong, especially when you keep mixing up variable names. Why would you push pure_string into settings_vector. If you write a small code example that compiles and gives the error you talk about it will be easier to help you.
This gets even more weird. Now, if I insert a space somewhere inside of the string it works. When I remove it this shit crashes again. I'll try to see this in a console then post the code
Last edited on
pure_string was dynamically allocated inside getfromfile, ¿right?
I don't see an error in your code.
getfromfile() returns a char-pointer.
But where does it point to?
Are you sure the pointed to data is still valid after returning from getfromfile()?
Who is resposible for it's allocation/deallocation?

The thing is that strtok() returns pointers into the original string data.
I would look for the problem there...
1
2
3
4
5
6
7
8
9
10
11
12
char* getfromfile(char* filename) {
     if ((fp = fopen(filename, "r")) != NULL) {  
        fseek(fp, 0, SEEK_END);
        len = ftell(fp);
        fseek(fp, 0, SEEK_SET);
        char* enc = (char *)malloc(len); 
        fread(enc, len, 1, fp);
        fclose(fp);
        enc[len] = 0;
        return enc;
     }
}


This is the getfromfile() function
Last edited on
You don't allocate space for the null character.
Peter07, I would to give you the biggest hug ever. Thanks man! Thank you everybody. It was that. For a "+1" I almost lose my minds. Thanks again. It seems like I have to be more patient when programming. Thanks, thanks, thanks!
Topic archived. No new replies allowed.