总是看大家的文章受益匪浅 今天也来发布一个 根据自己的代码整理出来的
(http://chenzhjlf.blog.sohu.com/219912295.html 搜狐的博客)
数据上传到服务器为字符串样式 需要提取数据并保存到数据库。
char* pInfo = "DD=90C,SS=0.7M,CC=6.1D,YY=152.1M";如果用函数需要进行多次判断 于是想到了用宏的方法 经过测试没有问题。
定义结构体
typedef struct _INFO
{
char szDD[24]; //
char szSS[24]; //
...
_INFO()
{
memset(this, 0, sizeof(_INFO));
}
}
#define GetItem(Item) \
{ \
char pSerach[4]; \
sprintf(pSerach, "%s=", #Item); \
char *pPos = strstr(pInfo, pSerach); \
if (NULL != pPos) \
{ \
pPos += 3; \
char* pStart = pPos; \
while ( (‘0’<= *pPos && ‘9' >= *pPos) || '.' == *pPos ) \
{ \
*pPos++; \
} \
strncpy( stuInfo.sz##Item, pStart, pPos-pStart); \
}
//char* pInfo = "DD=90M,SS=0.7M,CC=6.1M,YY=152.1M";
void GetItems(char* pInfo)
{
char* pPos = pInfo;
INFO stuInfo;
GetItem(DD);
GetItem(SS);
...
}
以上只是一个思路 前提是每一项的数据格式相同, 宏中没有对字符串的格式进行检查 如果格式不对可能会有异常。 如果您有好的方法请告之。