strtok() 的用法
Usage of strtok()
我尝试了我的 insertItem()
列表 api 成功地插入了所有类型的项目,例如,字符串(如下),
static void copyData(List *records, char *delim, char *argv[]){
char *item = malloc(5);
strcpy(item, "abcd");
int count = 0;
while(count++ < 10){
insertItem(records, item);
item = malloc(5);
strcpy(item, "efgh");
}
}
输出:
$ ./frequencyCounter.exe ARRAY
Key is:abcd
Key is:efgh
Key is:efgh
Key is:efgh
Key is:efgh
Key is:efgh
Key is:efgh
Key is:efgh
Key is:efgh
Key is:efgh
Size of list: 10
Before freeList()After freeList()
但是strtok()
tokens显示问题代码,如下图,buf[]
上面不需要参数,
static void copyData(List *records, char buf[], char *delim, char *argv[]){
char *token = strtok(buf, delim);
void *item = malloc(strlen(token) + 1);
item = memcpy(item, token, strlen(token)+1);
printf("token-%s\n", token);
while(token != NULL){
insertItem(records, item);
token = strtok(NULL, delim);
if(token != NULL){
printf("token-%s\n", token);
item = malloc(strlen(token) + 1); //every item has its own heap memory
memcpy(item, token, strlen(token)+1);
}
}
}
输出是,
$ ./frequencyCounter.exe ARRAY
token-it
token-was
token-the
token-best
token-of
token-times
Key is:it
Key is:it
Key is:it
Key is:it
Key is:it
Key is:it
Size of list: 6
Aborted (core dumped)
在问题代码(上面)中,我们可以重新使用 strtok()
的结果吗?
虽然很难理解或阅读您的代码,但以下内容看起来很糟糕
item = malloc(strlen(token));
你然后 memcpy()
strlen(token) + 1
字节。
这将导致未定义的行为,因此您的程序将表现不稳定。
另外,不要过度使用 strlen()
它会在您每次调用它时遍历输入字符串的所有字符,如果您需要多次使用它,则只需将结果存储在某个地方。
这是您的函数的改进版本[=19=]
static void
copyData(List *records, char buf[], char *delim, char *argv[])
{
char *token;
size_t length;
void *item;
token = strtok(buf, delim);
if (token == NULL)
return;
length = strlen(token);
item = malloc(length + 1);
if (item == NULL)
return;
memcpy(item, token, length + 1);
printf("token-%s\n", token);
while (token != NULL) {
insertItem(records, item);
token = strtok(NULL, delim);
if (token != NULL) {
length = strlen(token);
item = malloc(length + 1); // every item has its own heap memory
if (item != NULL) {
memcpy(item, token, length + 1);
}
printf("token-%s\n", token);
}
}
// Some day, you will have to free these pointers
}
但我希望不只是这样,因为每个 item
你 malloc()
永远不会 free
d 而且,因为它在某种程度上违反了 DRY 原则。还有一件事,你也可以使用 strdup()
。
我尝试了我的 insertItem()
列表 api 成功地插入了所有类型的项目,例如,字符串(如下),
static void copyData(List *records, char *delim, char *argv[]){
char *item = malloc(5);
strcpy(item, "abcd");
int count = 0;
while(count++ < 10){
insertItem(records, item);
item = malloc(5);
strcpy(item, "efgh");
}
}
输出:
$ ./frequencyCounter.exe ARRAY
Key is:abcd
Key is:efgh
Key is:efgh
Key is:efgh
Key is:efgh
Key is:efgh
Key is:efgh
Key is:efgh
Key is:efgh
Key is:efgh
Size of list: 10
Before freeList()After freeList()
但是strtok()
tokens显示问题代码,如下图,buf[]
上面不需要参数,
static void copyData(List *records, char buf[], char *delim, char *argv[]){
char *token = strtok(buf, delim);
void *item = malloc(strlen(token) + 1);
item = memcpy(item, token, strlen(token)+1);
printf("token-%s\n", token);
while(token != NULL){
insertItem(records, item);
token = strtok(NULL, delim);
if(token != NULL){
printf("token-%s\n", token);
item = malloc(strlen(token) + 1); //every item has its own heap memory
memcpy(item, token, strlen(token)+1);
}
}
}
输出是,
$ ./frequencyCounter.exe ARRAY
token-it
token-was
token-the
token-best
token-of
token-times
Key is:it
Key is:it
Key is:it
Key is:it
Key is:it
Key is:it
Size of list: 6
Aborted (core dumped)
在问题代码(上面)中,我们可以重新使用 strtok()
的结果吗?
虽然很难理解或阅读您的代码,但以下内容看起来很糟糕
item = malloc(strlen(token));
你然后 memcpy()
strlen(token) + 1
字节。
这将导致未定义的行为,因此您的程序将表现不稳定。
另外,不要过度使用 strlen()
它会在您每次调用它时遍历输入字符串的所有字符,如果您需要多次使用它,则只需将结果存储在某个地方。
这是您的函数的改进版本[=19=]
static void
copyData(List *records, char buf[], char *delim, char *argv[])
{
char *token;
size_t length;
void *item;
token = strtok(buf, delim);
if (token == NULL)
return;
length = strlen(token);
item = malloc(length + 1);
if (item == NULL)
return;
memcpy(item, token, length + 1);
printf("token-%s\n", token);
while (token != NULL) {
insertItem(records, item);
token = strtok(NULL, delim);
if (token != NULL) {
length = strlen(token);
item = malloc(length + 1); // every item has its own heap memory
if (item != NULL) {
memcpy(item, token, length + 1);
}
printf("token-%s\n", token);
}
}
// Some day, you will have to free these pointers
}
但我希望不只是这样,因为每个 item
你 malloc()
永远不会 free
d 而且,因为它在某种程度上违反了 DRY 原则。还有一件事,你也可以使用 strdup()
。