[問題] strtok求幫助

看板C_and_CPP (C/C++)作者 (opl)時間12年前 (2013/10/18 19:04), 編輯推噓0(009)
留言9則, 2人參與, 最新討論串1/1
s36m開發平台(Platform [36m開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 我想用strtok把buf上的資料(預計會是401 authorization=>1234) 切成 buf=401 authorization code=1234 這樣 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): error C2440: '=' : cannot convert from 'char *' to 'char [1024]' 好像是長度不符的關係? 程式碼(Code):(請善用置底文網頁, 記得排版) 程式很長 我擷取一部分有關的來說好了 char code[BUF_SZIE]; char buf[BUF_SZIE]; int main() { recvfrom(sClient, buf, BUF_SZIE, 0, (SOCKADDR*)&clientAddr, &nClientLen); buf=strtok(buf,"=>"); code=strtok(NULL," "); } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.230.121.205

10/18 19:20, , 1F
buf 是 array 不是 pointer, 不要拿 buf 接收傳回值
10/18 19:20, 1F

10/18 19:21, , 2F
宣告 char *ptr = buf; 再用 strtok() 操作 ptr 吧
10/18 19:21, 2F

10/18 19:50, , 3F
感謝一樓大大 現在新的問題是我code那邊好像寫得不對
10/18 19:50, 3F

10/18 19:53, , 4F
code顯示出來是亂碼@@
10/18 19:53, 4F

10/18 19:58, , 5F
檢查一下 strtok 是不是 return NULL
10/18 19:58, 5F

10/18 20:01, , 6F
對了, recvfrom 不會自動幫你結尾加 '\0'
10/18 20:01, 6F

10/18 20:02, , 7F
在recvfrom前面加memset(buf,0,1024);後面加buf[1023]=0;
10/18 20:02, 7F

10/18 20:03, , 8F
確定 buf 有正確的 '\0' 結尾才不會出現靈異現象
10/18 20:03, 8F

10/18 20:21, , 9F
感謝解答 這個問題已解決了
10/18 20:21, 9F
文章代碼(AID): #1IOHMcV4 (C_and_CPP)
文章代碼(AID): #1IOHMcV4 (C_and_CPP)