Re: [問題] strtok的問題

看板C_and_CPP作者 (相見不如懷念)時間15年前 (2009/05/06 10:27), 編輯推噓1(103)
留言4則, 3人參與, 最新討論串3/7 (看更多)
※ 引述《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

05/06 13:13, , 1F
token+strlen(token)+1 ?? 不一定對吧...
05/06 13:13, 1F

05/06 20:37, , 2F
某些情況未必對 但依照這個case是可以這樣使用的
05/06 20:37, 2F

05/06 20:37, , 3F
要不然就用sscanf或者自己寫一個 tokenizer 處理吧
05/06 20:37, 3F

05/07 09:10, , 4F
恩恩 謝謝
05/07 09:10, 4F
文章代碼(AID): #1A0FL-fC (C_and_CPP)
文章代碼(AID): #1A0FL-fC (C_and_CPP)