#include <stdio.h>#include <stdlib.h>#include <string.h>//根据空格拆分字符串 int partition(char *src, char *par, int pos){ int i,j; i = pos; //取得一个非空字符 while(src[i] == ' ') ++i; if(src[i] != '/0') { j = 0; while((src[i] != '/0') && (src[i] != ' ')) { //判断条件是否满足 if((src[i] > '9') || (src[i] < '0') && (src[i] != '-')) return -1 ; par[j] = src[i]; ++i; ++j; } par[j]='/0'; return i; } else return -1;}int main(void){ int serial_number ; int lenght ; int status ; int data_length; int data1,data2,data3; int position = 0; int para_flag = 1 ; int parameter_item = 0; char partition_string[20] = {0}; char *data = "00001 00007 1 3 34567 26358 32698"; while(para_flag) { if(para_flag == 0) break ; if((position = partition(data,partition_string,position)) != -1) { ++parameter_item ; switch(parameter_item) { case 1: serial_number = atoi(partition_string); break ; case 2: lenght = atoi(partition_string); break ; case 3: status = atoi(partition_string); break ; case 4: data_length = atoi(partition_string); break ; case 5: data1 = atoi(partition_string); break ; case 6: data2 = atoi(partition_string); break ; case 7: data3 = atoi(partition_string); para_flag = 0 ; break ; } } } printf("序号:%d/n",serial_number); printf("长度:%d/n",lenght); printf("状态字:%d/n",status); printf("数据长度:%d/n",data_length); printf("数据1:%d/n",data1); printf("数据2:%d/n",data2); printf("数据3:%d/n",data3); return 0; }