2010年2月17日

C語言取出token或字串的最簡單程式碼

就是用strtok這個標準的C函數, 範例如下:

#include <stdio.h>
#include <string.h>
int main(int argc,char **argv)
{
char * buf1="aaa, ,a, ,,,bbb-c,,,ee|abc";
/* Establish string and get the first token: */
char* token = strtok( buf1, ",-|");
while( token != NULL )
    {
/* While there are tokens in "string" */
        printf( "%s ", token );
/* Get next token: */
        token = strtok( NULL, ",-|");
    }
return 0;
}

沒有留言:

張貼留言