现在的位置: 首页 > 综合 > 正文

C strtok strtok_r

2017年11月12日 ⁄ 综合 ⁄ 共 1004字 ⁄ 字号 评论关闭

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

int main()
{
    /*  
    char str[16] = "host:12306";
    char *p;
    p   = strtok(str,":");
    if(p)
        printf("%s\n", p);

    p   = strtok(NULL, ",");
    if (p) printf("%s\n", p);
    */

    char str[]  = "a,b,c,d e f g";
    char *p; 
    p=strtok(str, ",");
    while(p)
    {   
        printf("%s\n",p);
        p=strtok(NULL, ",");
    }   

    return 0;
}

#include <iostream>

using namespace std;

int main()
{
    char ip_host[]  = "localhost:3306";
    char *toKenPtr  =strtok(ip_host, ":");
    while(toKenPtr!=NULL)
    {   
        cout << toKenPtr << endl;
        toKenPtr = strtok(NULL, ",");
    }   
    return 0;
}

#include <stdio.h>

#include <string.h>

int main()
{
    int in=0;
    char buffer[] ="host:1010\nip:192.168.1.1";
    char *p[200];
    char *buf=buffer;
    char *outer_ptr=NULL;
    char *inner_ptr=NULL;

    while((p[in]=strtok_r(buf,"\n",&outer_ptr))!=NULL)
    {
        buf=p[in];
        while((p[in]=strtok_r(buf,":", &inner_ptr))!=NULL)
        {
            in++;
            buf=NULL;
        }
        p[in++] = "***";
        buf=NULL;
    }
    printf("here we have %d strings\n",in);
    int j;
    for(j=0;j<in;j++)
        printf("%d\t>%s<\n", j,p[j]);
    return 0;
}

抱歉!评论已关闭.