字符串以指定符号分割

时间:2021-07-04 22:15:21

void MsgHandle::handleMsg(char* strMsg)
{
 char temp[512];
 strcpy(temp,strMsg);
 if (isValidMsg(temp))
 {
  ReplaceStr(temp,"|"," ");
  ReplaceStr(temp,"<","");
  ReplaceStr(temp,">","");
  sscanf(temp,"%d %s %s %s %s %s %s %s",&m_msgdata.MarkID,m_msgdata.ScrHost,m_msgdata.ScrDeviceType,m_msgdata.ScrDeviceNum,
   m_msgdata.DesHost,m_msgdata.DesDeviceType,m_msgdata.DesDeviceNum,m_msgdata.Command);
  //printf("hd<%d|%s|%s|%d|%s|%s|%d|%s>",m_msgdata.MarkID,m_msgdata.ScrHost,m_msgdata.ScrDeviceType,m_msgdata.ScrDeviceNum,
  //m_msgdata.DesHost,m_msgdata.DesDeviceType,m_msgdata.DesDeviceNum,m_msgdata.Command); 
 }
 else
 {
  printf("消息格式错误!\n");
 }
}

该代码的中使用的strMsg的格式:222|aaaa|kjijh|0000|等