首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

字符串制表符分解

2012-03-19 
字符串制表符分解求助读取文本里面的内容以后,原本打算使用 strtok 函数来分解字符串结果才发现举例(假设

字符串制表符分解求助
读取文本里面的内容以后,原本打算使用 strtok 函数来分解字符串结果才发现 举例(假设用 ","来代替制表符tab) : CString st= "a1,a2,,a4"; 这种情况的时候 strtok函数会忽略掉 中间没有内容的地方 直接就被忽略了变成: a1 a2 a4 而我要的是把中间的没有内容的地方也要保留 变成 a1 a2 a4 或者空缺的地方用其他表示也行 a1 a2 NULL a4 请高手帮忙 本人是新手 麻烦高手用代码说明

[解决办法]
http://topic.csdn.net/u/20081022/20/2766c46d-6ed3-4567-b0d9-bb9c4fc79236.html

要是不行就自己写分割函数吧,也不难写
[解决办法]

C/C++ code
#include <stdio.h>#include <string.h>char string[80];char seps1[3];char seps2[3];char *token;char *zzstrtok (    char *string,    const char *control1,//连续出现时视为中间夹空token    const char *control2 //连续出现时视为中间无空token    ){    unsigned char *str;    const unsigned char *ctrl1 = control1;    const unsigned char *ctrl2 = control2;    unsigned char map1[32],map2[32];    static char *nextoken;    static char flag=0;    unsigned char c;    int L;    memset(map1,0,32);    memset(map2,0,32);    do {        map1[*ctrl1 >> 3] |= (1 << (*ctrl1 & 7));    } while (*ctrl1++);    do {        map2[*ctrl2 >> 3] |= (1 << (*ctrl2 & 7));    } while (*ctrl2++);    if (string) {        if (control2[0]) {            L=strlen(string);            while (1) {                c=string[L-1];                if (map2[c >> 3] & (1 << (c & 7))) {                    L--;                    string[L]=0;                } else break;            }        }        if (control1[0]) {            L=strlen(string);            c=string[L-1];            if (map1[c >> 3] & (1 << (c & 7))) {                string[L]=control1[0];                string[L+1]=0;            }        }        str=string;    }    else        str=nextoken;    string=str;    while (1) {        if (0==flag) {            if (!*str) break;            if (map1[*str >> 3] & (1 << (*str & 7))) {                *str=0;                str++;                break;            } else if (map2[*str >> 3] & (1 << (*str & 7))) {                string++;                str++;            } else {                flag=1;                str++;            }        } else if (1==flag) {            if (!*str) break;            if (map1[*str >> 3] & (1 << (*str & 7))) {                *str=0;                str++;                flag=0;                break;            } else if (map2[*str >> 3] & (1 << (*str & 7))) {                *str=0;                str++;                flag=2;                break;            } else str++;        } else {//2==flag            if (!*str) return NULL;            if (map1[*str >> 3] & (1 << (*str & 7))) {                str++;                string=str;                flag=0;            } else if (map2[*str >> 3] & (1 << (*str & 7))) {                str++;                string=str;            } else {                string=str;                str++;                flag=1;            }        }    }    nextoken=str;    if (string==str) return NULL;    else             return string;}void main(){   strcpy(string,"A \tstring\t\tof ,,tokens\n\nand some  more tokens, ");   strcpy(seps1,",\n");strcpy(seps2," \t");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,"1234| LIYI|China | 010 |201110260000|OK");   strcpy(seps1,"|");strcpy(seps2," ");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,"1234|LIYI||010|201110260000|OK");   strcpy(seps1,"");strcpy(seps2,"|");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,"1234|LIYI||010|201110260000|OK");   strcpy(seps1,"|");strcpy(seps2,"");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,"a");   strcpy(seps1,",");strcpy(seps2,"");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,"a,b");   strcpy(seps1,",");strcpy(seps2,"");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,"a,,b");   strcpy(seps1,",");strcpy(seps2,"");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,",a");   strcpy(seps1,",");strcpy(seps2,"");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,"a,");   strcpy(seps1,",");strcpy(seps2,"");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,",a,,b");   strcpy(seps1,",");strcpy(seps2,"");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,",,a,,b,,");   strcpy(seps1,",");strcpy(seps2,"");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,",");   strcpy(seps1,",");strcpy(seps2,"");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,",,");   strcpy(seps1,",");strcpy(seps2,"");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }   strcpy(string,",,,");   strcpy(seps1,",");strcpy(seps2," ");   printf("\n[%s]\nTokens:\n",string);   token=zzstrtok(string,seps1,seps2);   while (token!=NULL) {      printf(" <%s>",token);      token=zzstrtok(NULL,seps1,seps2);   }}////[A      string          of ,,tokens////and some  more tokens,]//Tokens:// <A>, <string>, <of>, <>, <tokens>, <>, <and>, <some>, <more>, <tokens>, <>,//[1234| LIYI|China | 010 |201110260000|OK]//Tokens:// <1234>, <LIYI>, <China>, <010>, <201110260000>, <OK>,//[1234|LIYI||010|201110260000|OK]//Tokens:// <1234>, <LIYI>, <010>, <201110260000>, <OK>,//[1234|LIYI||010|201110260000|OK]//Tokens:// <1234>, <LIYI>, <>, <010>, <201110260000>, <OK>,//[a]//Tokens:// <a>,//[a,b]//Tokens:// <a>, <b>,//[a,,b]//Tokens:// <a>, <>, <b>,//[,a]//Tokens:// <>, <a>,//[a,]//Tokens:// <a>, <>,//[,a,,b]//Tokens:// <>, <a>, <>, <b>,//[,,a,,b,,]//Tokens:// <>, <>, <a>, <>, <b>, <>, <>,//[,]//Tokens:// <>, <>,//[,,]//Tokens:// <>, <>, <>,//[,,,]//Tokens:// <>, <>, <>, <>, 


[解决办法]
看看这个怎么样

C/C++ code
char *strtok_he(const char *src, char *demial,unsigned int n){    char map[32];      int count;    int i = 0;    char* str = strdup(src);    char* ctrl = demial;    char *str_org;    if (src == NULL || demial == NULL)    {        return NULL;    }    for (count =0; count <32; count++)    {        map[count] = 0;    }    while (*ctrl)    {        map[*ctrl >> 3] |= (1 << (*ctrl & 7));        ++ctrl;    }    str_org = str;    for (;*str; str++)    {        if ( map[*str >> 3] & (1 << (*str & 7)))        {            if (i == n)            {                *str++ = '\0';                break;            }            ++i;            str_org = str+1;        }    }    if (i<n)    {        return NULL;    }    return str_org;}int main(){    char buf[100] = "ca,bdfbwa,fef,,b,c,dfs";    char *p = NULL;    int i = 0;    p = strtok_he(buf,",",0);     while (p != NULL)    {        cout << p << endl;        i++;        p = strtok_he(buf,",",i);    }    }
[解决办法]
C/C++ code
CString str = _T("a1,a2,,a4");    LPCTSTR lpszToken = _T(",");    CString strText(str);    int nFlag = -1;    CString tmp(_T(""));    while(TRUE)    {        nFlag = strText.Find(lpszToken);        if(-1 == nFlag)        {            if(!strText.IsEmpty())                tmp = strText;            break;        }        tmp = strText.Left(nFlag);        AfxMessageBox(tmp);                strText = strText.Right(strText.GetLength() - nFlag - 1);    }    AfxMessageBox(tmp);
[解决办法]
System::String st = "a1,a2,,a4";
System::StringArray ss = st.Split(",");
ss就是分割后的结果

热点排行