1

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;
0

2 Answers 2

5

You need a null terminator at the end of your cstring.

Sign up to request clarification or add additional context in comments.

Comments

1

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';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.