字符串挖掘数据到结构体

  1 #include <iostream>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 char str[1024]="569603080----zj123456789 
  6 94940443----zj123456789 
  7 260024036----zj1456789 
  8 707611428----zj123456789 
  9 793516568----zj123456789 
 10 280105138----zj1256789 
 11 853339297----zj145678 
 12 791088021----zj1234789 
 13 461913547----zj12366456789 
 14 739195069----zj12389 
 15 595061094----zj123789 
 16 835928755----zj123456789 
 17 824218951----zj123456789 
 18 824219738----zj123456789 
 19 824220392----zj16789 
 20 793708834----zj123456789 
 21 462942630----zj1789 
 22 779446648----zj1256789 
 23 807517937----zj123456789 
 24 785332401----zj123456789 
 25 824637233----zj123456789 
 26 824637654----zj126789 
 27 824642464----zj123456789 
 28 657004476----zj123456789 
 29 550261273----zj16789 
 30 771451402----zj123456789 
 31 478838706----zj123456789 
 32 837569545----zj12345789 
 33 838778566----zj12789 
 34 838466198----zj126789 
 35 807522015----zj1236789 
 36 823194447----zj12789 
 37 295936400----zj123456789 
 38 838041792----zj1239 
 39 838042137----zj123456789 
 40 838042825----zj1289 ";
 41 
 42 struct info
 43 {
 44     char *QQ;
 45     char *password;
 46 };
 47 
 48 struct infos
 49 {
 50     struct info *pinfo;
 51     int length;
 52 }myinfos;
 53 
 54 int getnum(char *str)
 55 {
 56     int i=0;
 57     for(char *p = strstr(str,"----");p!=NULL;p = strstr(p+4,"----"))
 58     {
 59         i++;
 60     }
 61     return i;
 62 }
 63 
 64 void initmem()
 65 {
 66     int length = getnum(str);
 67     myinfos.pinfo = (struct info *)malloc(sizeof(struct info)*length);
 68     myinfos.length = length;
 69 }
 70 
 71 void initdata(char *str)
 72 {
 73     int length = strlen(str);
 74     for(char *p = str;p<str+length;p++)
 75     {
 76 
 77         if(*p==' ')
 78         {
 79             *p = '';
 80         }
 81     }
 82 
 83     for(char *px = str,ch = 0; px < str+length ;ch++)
 84     {
 85         //printf("%s
",px);
 86         char *pnew = (char *)malloc(sizeof(char)*strlen(px)+1);
 87         strcpy(pnew,px);
 88         char *pwei = strstr(pnew,"----");
 89         *pwei = '';
 90         myinfos.pinfo[ch].QQ=(char *)malloc(sizeof(char)*strlen(pnew)+1);
 91         myinfos.pinfo[ch].password=(char *)malloc(sizeof(char)*strlen(pwei+4)+1);
 92         sprintf(myinfos.pinfo[ch].QQ,"%s",pnew);
 93         sprintf(myinfos.pinfo[ch].password,"%s",pwei+4);
 94         px += strlen(px) + 1;
 95     }
 96 }
 97 
 98 void showdata()
 99 {
100     for(int i=0;i<myinfos.length;i++)
101     {
102         printf("QQ=%ld,password=%s
",myinfos.pinfo[i].QQ,myinfos.pinfo[i].password);
103     }
104 }
105 
106  void main()
107 {
108     initmem();
109     initdata(str);
110     showdata();
111     printf("%d",getnum(str));
112 }
原文地址:https://www.cnblogs.com/xiaochi/p/5381975.html