作者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
→ visor:token+strlen(token)+1 ?? 不一定對吧... 05/06 13:13
→ LiloHuang:某些情況未必對 但依照這個case是可以這樣使用的 05/06 20:37
→ LiloHuang:要不然就用sscanf或者自己寫一個 tokenizer 處理吧 05/06 20:37
推 ckai1983:恩恩 謝謝 05/07 09:10