• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Question for C programmers out there..

blahblah99

Platinum Member
I have a string (a line read from a file) that needs to be tokenized... it is in CSV format, and I need a function that returns an array. I know in php there's an EXPLODE() function, but what about C?

For example,

char *tokenize(char *string, char delimiter) {
//function code goes here
}

char line[] = "book, yellow, hardcover, isbn, author, price";

array = tokenize(line,",");

where array returned would be:

array[0] = "book";
array[1] = "yellow";
array[2] = "hardcover";
array[3] = "isbn";
array[4] = "author";
array[5] = "price";

Thanks in advance!
 
Something like this is what you're looking for. You'll have to replace the c++ strings and vectors with their c equivalents, but that shouldn't be too difficult.

void TokenizeString(vector<string> & tokens, const char * pString, const char * pDelimiters)
{
char *pNextWordStart = pString + strspn(pString, pDelimiters);

while (*pNextWordStart != '\0')
{
const size_t wordEndIndex = strcspn(pNextWordStart, pDelimiters);
tokens.push_back(string(pNextWordStart, wordEndIndex));
pNextWordStart += wordEndIndex;
pNextWordStart += strspn(pNextWordStart, pDelimiters);
}
}

call it like so: TokenizeString(storage, "book, yellow, hardcover, isbn, author, price", ", \t\n");
 
Back
Top