Reputation: 1419
I've seen all the parsing examples, but they're all either using string or other random methods that wouldn't fix my problem. My problem is that, when I read from a text file, the tokens I extract come out with the token itself plus a bunch of random letters and symbols. For example a line in the text file reads ... create_device digital_controller "Left Turn Lamp" 51 ... and I want to be able to point to each token with my pointer array *tklist[]. However when I parse and point, I get - ... create_deviceýýýý««««««««þîþîþîþ ... Along with getting each other token in a similar fashion. Here is my code for extracting the tokens. Assume that my token positions are correct because I've check and double checked that the positions are where they should be, otherwise I would only be getting part of the word I want included in the mess of characters. cline is declared as 'char cline[]' and is the array of characters for a line
token_length = endTokenPosition - startTokenPosition; //length of the token
tklist[next_token] = (char *)malloc(token_length + 1);
memcpy(tklist[next_token], &cline[startTokenPosition], token_length + 1);
cout << tklist[next_token] << endl;
Upvotes: 1
Views: 606
Reputation: 48280
You've copied the token into tklist[next_token]
but haven't terminated it with a null character, so cout
continues past the end. You could try either:
memset(tklist[next_token], '\0', token_length + 1);
memcpy(tklist[next_token], &cline[startTokenPosition], token_length);
or
memcpy(tklist[next_token], &cline[startTokenPosition], token_length);
tklist[text_token][token_length] = '\0';
Upvotes: 1