作者LiloHuang (相见不如怀念)
看板C_and_CPP
标题Re: [问题] strtok的问题
时间Wed May 6 10:27:09 2009
※ 引述《ckai1983 ( )》之铭言:
: 请问如果我的字串是
: char *buf = "AA \"123 456\"";
这边应该改成 const char *buf = "AA \"123 456\"";
右边那串是 read-only 的 text section
你如果要可以修改应该写 char buf[] = "AA \"123 456\"";
: 用char* token = strtok( buf, " ");
: token 是 AA
: 我要怎麽取得剩下的 "123 456" 字串?
: 谢谢!
#include <stdio.h>
#include <string.h>
int main() {
char buf[] = "AA \"123 456\"";
char* token = strtok(buf, " ");
printf("first %s\n", token);
printf("next %s\n", token+strlen(token)+1);
return 0;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 211.79.63.18
1F:→ visor:token+strlen(token)+1 ?? 不一定对吧... 05/06 13:13
2F:→ LiloHuang:某些情况未必对 但依照这个case是可以这样使用的 05/06 20:37
3F:→ LiloHuang:要不然就用sscanf或者自己写一个 tokenizer 处理吧 05/06 20:37
4F:推 ckai1983:恩恩 谢谢 05/07 09:10