码迷,mamicode.com
首页 > 编程语言 > 详细

C 语言sscanf

时间:2016-07-11 12:01:39      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

C语言以sscanf逗号作为分割符

    char buf_a[10]={0};
    char buf_c[10]={0};
    char buf_b[5]={0};

   sscanf("/dev/ttyS0,115200,8,1,n","%[^,],%[^,],%[^,],%[^,],%s",buf_a,buf_c,&buf_b[1],&buf_b[2],&buf_b[3]);
   printf("*****************\n");
    printf("%s\n", buf_a);
    printf("%s\n", buf_c);
    printf("%c\n", buf_b[1]);
    printf("%c\n", buf_b[2]);
    printf("%c\n", buf_b[3]);

以strtok作为分割符

   char str[]="/dev/ttyS0,115200,8,1,n";
  char *result=NULL;
  result=strtok(str,",");
  while(result!=NULL){
    printf("result is \"%s\"\n",result);
    result = strtok(NULL,",");
  }

C++ 逗号分割符

  
  

vector<string> split(string str, string pattern){

string::size_type pos;
vector<string> result;
str+=pattern;
int size = str.size();
for(int i=0; i<size; i++){
pos = str.find(pattern,i);
if(pos<size){
string s = str.substr(i,pos-i);
result.push_back(s);
i=pos+pattern.size()-1;
}
}

return result;
}




std::string pattern=",";
std::string str="/dev/ttyS0,115200,8,1,n";
std::vector<std::string>result = split(str,pattern);


for(int i=0;i<result.size();i++){
cout<<"aaa:"<<result[i]<<endl;
}

 

 

C 语言sscanf

标签:

原文地址:http://www.cnblogs.com/hzijone/p/5659696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!