【发布时间】:2011-05-04 17:26:10
【问题描述】:
我正在尝试对一条线进行标记并将其放入一个二维数组中,到目前为止我已经想出了这个,但我觉得我离得很远:
/**
* Function to tokenize an input line into seperate tokens
*
* The first arg is the line to be tokenized and the second arg points to
* a 2-dimentional string array. The number of rows of this array should be
* at least MAX_TOKENS_PER_LINE size, and the number of columns (i.e., length
* of each string should be at least MAX_TOKEN_SIZE)
*
* Returns 0 on success and negative number on failure
*/
int __tokenize(char *line, char tokens[][MAX_TOKEN_SIZE], int *num_tokens){
char *tokenPtr;
tokenPtr = strtok(line, " \t");
for(int j =0; j<MAX_TOKEN_SIZE; j++){
while(tokenPtr != NULL){
if(!(tokens[][j] = tokenPtr)){return -1;}
num_tokens++;
tokenPtr = strtok(NULL, " \t");
}
}
return 0;
}
【问题讨论】:
-
我认为您可能需要编辑该帖子,因为问题似乎并不完整。
-
strtok接受 2 个参数。您使用的是什么系统/语言(带有 3 个参数strtok)? -
在 C 中,strtok 通常分两步使用:首先初始化 (
strtok(INPUT_STRING, DELIMITERS)),然后在循环中抓取更多块 (strtok(NULL, DELIMITERS))。 -
如何列出多个分隔符?
标签: c pointers multidimensional-array strtok