Posted on 2010-02-08 09:54
Prayer 閱讀(1322)
評論(3) 編輯 收藏 引用 所屬分類:
C/C++
從C#、JAVA到C ,讓我覺得像是從公產(chǎn)主義社會回到了原始社會,不順手,所以很心里憋氣!!!
函數(shù)名: strtok
功 能: 查找由在第二個串中指定的分界符分隔開的單詞
用 法: char *strtok(char *str1, char *str2);
程序例:

#include <string.h>
#include <stdio.h>

int main(void)


{
char input[16] = "abc,d";
char *p;


/**//* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, ",");
if (p) printf("%s\n", p);


/**//* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, ",");
if (p) printf("%s\n", p);
return 0;
}

下面是惡心的strtok函數(shù),用作分割字符串
它的第二個參數(shù)不能是char*的變量,是char[]的變量則沒問題
FUCK!
我做了N多次測試才發(fā)現(xiàn)的這個問題,害得我調(diào)試UNIX網(wǎng)絡編程作業(yè)好長時間
這個函數(shù)的調(diào)用真是奇怪
第一次要設定參數(shù),第二次參數(shù)竟然可以NULL
然后函數(shù)竟然能分割第一次參數(shù)設置的字符
我無語了
這樣的設計也太不人性化了
下面認真的說一下這個函數(shù),如有哪個和我一樣郁悶的人發(fā)現(xiàn)這篇文章,或許能少走些彎路
函數(shù)第一次調(diào)用需設置兩個參數(shù),
strtok(str,",") str 需要分割的串 “,”根據(jù),分割
第一次分割的結果,返回串中第一個,之前的字串,也就是上面的程序第一次輸出abc
第二次調(diào)用該函數(shù)
strtok(NULL,"."),第一個參數(shù)設置為NULL,第二個參數(shù)還是分割的依據(jù)
結果返回分割依據(jù)后面的字串,即上面的程序輸出d