[問題] strtok的問題

看板C_and_CPP作者 (longoria)時間13年前 (2010/11/28 22:00), 編輯推噓2(206)
留言8則, 4人參與, 最新討論串7/7 (看更多)
遇到的問題: (題意請描述清楚) strtok讀取結果未如預期 希望得到的正確結果: In the class, store:1,05:11:11,06:50:12 In the class, name: 1 In the class, starttime: 05:11:11 In the class, endtime: 06:50:12 程式跑出來的錯誤結果: In the class, store:1,05:11:11,06:50:12 In the class, name: 1 In the class, starttime: , In the class, endtime: , 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) visual studio 2008 有問題的code: (請善用置底文標色功能) void cTrainTime::setTrain(char *n) { char *tempname, *tempstart, *tempend; printf("In the class, store:%s\n",n); tempname = strtok(n,","); tempstart = (NULL, ","); tempend = (NULL, ","); printf("In the class, name:%s \n",tempname); printf("In the class, starttime:%s \n",tempstart); printf("In the class, endtime:%s \n",tempend); }; 補充說明: 請問各位先進 以上程式碼中的觀念是否出錯 怎麼會跑出" , " QQ 另外,我想針對上述的時間以秒的方式做計算, 但是他們目前狀態是char 要怎麼轉換比較方便呢? (現在只想到一個字一個字對照ASCII換 @@) 謝謝各位了^^ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.122.114.59

11/28 22:05, , 1F
= strtok(NULL,","); compiler 沒那麼聰明知道你要連用XD
11/28 22:05, 1F

11/28 22:05, , 2F
後一問題 有 sscanf 或 atoi 可以用
11/28 22:05, 2F

11/28 22:11, , 3F
如果我要連用 大概怎麼修正 (我好像對strtok會錯意了XD)
11/28 22:11, 3F

11/28 22:57, , 4F
XDDD 你這樣是回傳後面的那個","阿
11/28 22:57, 4F

11/28 23:00, , 5F
有點蠢QQ 我竟然在這裡卡了那麼久= =
11/28 23:00, 5F

11/29 00:06, , 6F
呃 我不是寫在一樓的開頭了嗎 orz
11/29 00:06, 6F

11/29 00:52, , 7F
其實我會建議你 不如就直接去看impl就好了 libc/strtok.c
11/29 00:52, 7F

11/29 01:08, , 8F
L大 sorry 一時沒注意QQ
11/29 01:08, 8F
文章代碼(AID): #1Cyc24PJ (C_and_CPP)
文章代碼(AID): #1Cyc24PJ (C_and_CPP)