#include <stdio.h>
#include <string.h>
char s[]="direct usb://Canon/MX420%20series?serial=312E7C&interface=1 \"Canon MX420 series\" \"Canon MX420 series\" \"MFG:Canon;CMD:BJL,BJRaster3,BSCCe,NCCe,IVEC,IVECPLI;SOJ:BJNP2,BJNPe;MDL:MX420 series;CLS:PRINTER;DES:Canon MX420 series; VER:1.020;STA:10;FSI:04;HRI:CHN;MSI:DAT,E3,HASF;PDR:7;PSE:LFBA05000;";
char *p;
char MFG[40];
char MDL[40];
char DES[40];
int main() {
p=strstr(s,"MFG:");if (p && 1==sscanf(p,"MFG:%39[^;]",MFG)) printf("MFG=[%s]\n",MFG);
p=strstr(s,"MDL:");if (p && 1==sscanf(p,"MDL:%39[^;]",MDL)) printf("MDL=[%s]\n",MDL);
p=strstr(s,"DES:");if (p && 1==sscanf(p,"DES:%39[^;]",DES)) printf("DES=[%s]\n",DES);
return 0;
}
//MFG=[Canon]
//MDL=[MX420 series]
//DES=[Canon MX420 series]
//
另外,自己实现strstr()函数简单代码:
- const char *my_strstr(const char *str, const char *sub_str)
- {
- for(int i = 0; str[i] != '\0'; i++)
- {
- int tem = i; //tem保留主串中的起始判断下标位置
- int j = 0;
- while(str[i++] == sub_str[j++])
- {
- if(sub_str[j] == '\0')
- {
- return &str[tem];
- }
- }
- i = tem;
- }
- return NULL;
- }
- int main()
- {
- char *s = "1233345hello";
- char *sub = "345";
- printf("%s\n", my_strstr(s, sub));
- return 0;
- }