拆分字符串 StringToken

在c的库中有按字符拆分的函数

先写两个库中的函数,在读取时需要用到的。

extern char *strpbrk(const char *s1, const char *s2);
char strpbrk(const char * cs,const char * ct)
{
    const char *sc1,*sc2;
    for( sc1 = cs; *sc1 != '\0'; ++sc1)
    {
        for( sc2 = ct; *sc2 != '\0'; ++sc2)
        {
            if (*sc1 == *sc2)
            {
                return (char *) sc1;
            }
        }
    }
    return NULL;
}

int strspn(const char *s,const char *accept)
{
    const char *p;
    const char *a;
    int count = 0;
    for(p = s; *p != '\0'; ++p)
    {
        for (a = accept; *a != '\0'; ++a)
        {
            if (*p == *a)
            {
                break;
            }
        }//里面的<a target="_blank" href="/subview/1042560/1042560.htm">for循环</a>到此为止
        if (*a == '\0')
        {
            return count;
        }
        ++count;
    }//外面的for循环到此为止
    return count;
}
这个就是按字符拆分函数

strtok_r(在windows中是不存在这个函数的,只有linux中有)

    /* Parse S into tokens separated by characters in DELIM.
       If S is NULL, the saved pointer in SAVE_PTR is used as
       the next starting point.  For example:
            char s[] = "-abc-=-def";
            char *sp;
            x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
            x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
            x = strtok_r(NULL, "=", &sp);   // x = NULL
                    // s = "abc\0-def\0"
    */  
    char *strtok_r(char *s, const char *delim, char **save_ptr) {  
        char *token;  
      
        if (s == NULL) s = *save_ptr;  
      
        /* Scan leading delimiters.  */  
        s += strspn(s, delim);  
        if (*s == '\0')   
            return NULL;  
      
        /* Find the end of the token.  */  
        token = s;  
        s = strpbrk(token, delim);  
        if (s == NULL)  
            /* This token finishes the string.  */  
            *save_ptr = strchr(token, '\0');  
        else {  
            /* Terminate the token and make *SAVE_PTR point past it.  */  
            *s = '\0';  
            *save_ptr = s + 1;  
        }  
      
        return token;  

    } 


char *strtok(char *s, const char *delim)  
{  
    static char *last;  

    return strtok_r(s, delim, &last);  
}

以上的函数只能实现单字符的拆分

然后我们把strpbrk,strtok_r改一下。

char * mystrpbrk(const char * cs,const char * ct)
{
    const char *sc1,*sc2, *sc3;
    for( sc1 = cs; *sc1 != '\0'; ++sc1)
    {
        sc2 = ct;
        if(*sc1==*sc2)
        {
            for(sc3 = sc1;;++sc2,++sc3)
            {
                if(*sc2 == '\0')
                    return (char *)sc1;
                if(*sc3 != *sc2)
                    break;
            }
        }
    }
    return NULL;
}

char *mystrtok_r(char *s, const char *delim, char **save_ptr) {  
    char *token;  

    if (s == NULL) s = *save_ptr;  

    /* Scan leading delimiters.  */  
    //s += mystrspn(s, delim);  
    if (*s == '\0')   
        return NULL;  

    /* Find the end of the token.  */  
    token = s;  
    s = mystrpbrk(token, delim);  
    if (s == NULL)  
        /* This token finishes the string.  */  
        *save_ptr = strchr(token, '\0');  
    else {  
        /* Terminate the token and make *SAVE_PTR point past it.  */  
        *s = '\0';  
        *save_ptr = s + strlen(delim);  
    }  

    return token;  

}

以上改好后,我们就可以按字符拆分了。

        char s[] =",Fred male 25,John male 62,Anna female 16";
        char* delim = "male ";  
        char* tmp;  
        tmp = mystrtok(s, delim);  
        while(tmp != NULL)  
        {     
            printf("%s\n",tmp);
            tmp = mystrtok(NULL, delim);     
        }


strtok和strtok_r,具体解析请参考:

http://blog.csdn.net/liuintermilan/article/details/6280816

http://blog.csdn.net/liuintermilan/article/details/6283705


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MICROSOFT FOUNDATION CLASS LIBRARY : ZSCPascal AppWizard has created this ZSCPascal application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your ZSCPascal application. ZSCPascal.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CZSCPascalApp application class. ZSCPascal.cpp This is the main application source file that contains the application class CZSCPascalApp. ZSCPascal.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Developer Studio. resSCPascal.ico This is an icon file, which is used as the application s icon. This icon is included by the main resource file ZSCPascal.rc. resSCPascal.rc2 This file contains resources that are not edited by Microsoft Developer Studio. You should place all resources not editable by the resource editor in this file. ZSCPascal.reg This is an example .REG file that shows you the kind of registration settings the framework will set for you. You can use this as a .REG file to go along with your application or just delete it and rely on the default RegisterShellFileTypes registration. ZSCPascal.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. For the main frame window: MainFrm.h, MainFrm.cpp These files contain the frame class CMainFrame, which is derived from CMDIFrameWnd and controls all MDI frame features. resToolbar.bmp This bitmap file is used to create tiled images for the toolbar. The initial toolbar and status bar are constructed in the CMainFrame class. Edit this toolbar bitmap along with the array in MainFrm.cpp to add more toolbar buttons. AppWizard creates one document type and one view: ZSCPascalDoc.h, ZSCPascalDoc.cpp - the document These files contain your CZSCPascalDoc class. Edit these files to add your special document data and to implement file saving and loading (via CZSCPascalDoc::Serialize). ZSCPascalView.h, ZSCPascalView.cpp - the view of the document These files contain your CZSCPascalView class. CZSCPascalView objects are used to view CZSCPascalDoc objects. resSCPascalDoc.ico This is an icon file, which is used as the icon for MDI child windows for the CZSCPascalDoc class. This icon is included by the main resource file ZSCPascal.rc. Other standard files: StdAfx.h, StdAfx.cpp These files are used to build a precompiled header (PCH) file named ZSCPascal.pch and a precompiled types file named StdAfx.obj. Resource.h This is the standard header file, which defines new resource IDs. Microsoft Developer Studio reads and updates this file. Other notes: AppWizard uses "TODO:" to indicate parts of the source code you should add to or customize. If your application uses MFC in a shared DLL, and your application is in a language other than the operating system s current language, you will need to copy the corresponding localized resources MFC40XXX.DLL from the Microsoft Visual C++ CD-ROM onto the system or system32 directory, and rename it to be MFCLOC.DLL. ("XXX" stands for the language abbreviation. For example, MFC40DEU.DLL contains resources translated to German.) If you don t do this, some of the UI elements of your application will remain in the language of the operating system.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值