锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
#include <stdio.h>
int main(void)
{
char input[16] = "abc,dhh,eee";
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);
p = strtok(NULL, ",");
if (p) printf("%s\n", p);
return 0;
}
MSDN涓婄殑鍘熻瘽錛?
On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.
絎簩嬈″弬鏁扮珶鐒跺彲浠ULL
鏄洜涓簊trtok涓敤static鎸囬拡璁頒綇浜嗕笂嬈″鐞嗗悗鐨勪綅緗?br>
鎴戞兂鏄洜涓鴻繖涓嚱鏁板唴閮ㄥ疄鐜版椂錛岀敤鍒頒簡闈欐佸彉閲忥紝鑰岃涓嶈淇敼榪欎釜鍙橀噺錛屽氨鏄鏍規嵁絎竴涓弬鏁版潵紜畾錛?
褰撲負NULL鏃訛紝灝變笉鍐嶄慨鏀筍tatic鍙橀噺鐨勫間簡錛?
榪欎釜闈欐佸彉閲忕殑浣滅敤錛屽氨鏄褰曞師濮嬪瓧絎︿覆鐨勯暱搴︾殑錛?
using namespace std;
enum language{java, c, pascal, fortran, vc, vb, cpp} lang;
char name[][10] = {
"java",
"c",
"pascal",
"fortran",
"vc",
"vb",
"cpp"
};
int main()
{
lang = fortran;
cout << name[lang] << endl;
lang = java;
cout << name[lang] << endl;
lang = pascal;
cout << name[lang] << endl;
lang = (language) 6;//榪欎釜寮鴻漿緇忓吀
cout << name[lang] << endl;
return 0;
}