使用sscanf灵活处理字符串

sscanf() - 从一个字符串中读进与指定格式相符的数据。

函数原型:int sscanf(const char *str, const char *format, ...);

sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源。
  注:
  1、 * 亦可用于格式中, (即 %*d 和 %*s) 加了星号 (*) 表示跳过此数据不读入. (也就是不把此数据读入参数中)
  2、{a|b|c}表示a,b,c中选一,[d],表示可以有d也可以没有d。
  3、width表示读取宽度。
  4、{h | l | I64 | L}:参数的size,通常h表示单字节size,I表示2字节 size,L表示4字节size(double例外),l64表示8字节size。
  5、type :这就很多了,就是%s,%d之类。
  6、特别的:%*[width] [{h | l | I64 | L}]type 表示满足该条件的被过滤掉,不会向目标参数中写入其中的format可以是一个或多个 {%[*] [width] [{h | l | I64 | L}]type | ' ' | '\t' | '\n' | 非%符号}


支持集合操作:
  %[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配)
  %[aB'] 匹配a、B、'中一员,贪婪性
  %[^a] 匹配非a的任意字符,贪婪性

1. 常见用法。
  char buf[512] = ;
  sscanf("123456 ", "%s", buf);
  printf("%s\n", buf);
  结果为:123456
  2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。
  sscanf("123456 ", "%4s", buf);
  printf("%s\n", buf);
  结果为:1234
  3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。
  sscanf("123456 abcdedf", "%[^ ]", buf);
  printf("%s\n", buf);
  结果为:123456
  4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。
  sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf);
  printf("%s\n", buf);
  结果为:123456abcdedf
  5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。
  sscanf("123456abcdedfBCDEF", "%[^A-Z]", buf);
  printf("%s\n", buf);
  结果为:123456abcdedf

举例:从shell命令行输入命令,保存输出数据,从输出数据中寻找自己要找的数据。

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
static int shell_ouput(const char *cmd, char *buf, int buf_len)
{
    FILE *fp;
        size_t len;

            fp = popen(cmd, "r");
                if (fp == NULL) {
                  printf("popen failed.");
                   return -1;
                   }
        len = fread(buf, 1, buf_len, fp);
        if (len == 0) {
        printf("No output info for cmd: %s.", cmd);
        buf[buf_len - 1] = '\0';
        pclose(fp);
        return 0;
             }
        buf[buf_len - 1] = '\0';
        pclose(fp);

        return 0;
            }
 int main(){
 int ret =0;
 unsigned int val;
 char  cmd_buf[128] = { 0 };
    char  out_buf[2000] = { 0 };
 snprintf(cmd_buf, sizeof(cmd_buf), "sensors");
 ret = shell_ouput(cmd_buf, out_buf, sizeof(out_buf));
     if (ret != 0)
         {
        printf("cannot get output of cmd: %s\n", cmd_buf);
          return ret;
         }

printf("out_buf=%s\n",out_buf);
char *tmp_buf = out_buf;

      if(tmp_buf = strstr(tmp_buf, "System temp"))
          {
          printf("temp_buf=%s\n",tmp_buf);
                  tmp_buf += strlen("System temp ");
          printf("tmp_buf=%s\n",tmp_buf);
                          sscanf(tmp_buf, "%*[^0-9+-]%d", &val);
          }
          printf("val = %d\n",val);
return 0;
 }
编译后运行情况:
out_buf=coretemp-isa-0000
Adapter: ISA adapter
Core 0:       +49.0 C  (crit = +100.0 C)

coretemp-isa-0001
Adapter: ISA adapter
Core 1:       +52.0 C  (crit = +100.0 C)

w83627dhg-isa-0a10
Adapter: ISA adapter
Vcore:        +1.17 V  (min =  +0.00 V, max =  +1.74 V)
+12V:        +29.95 V  (min = +16.77 V, max =  +1.92 V)  ALARM
3.3V 3VCC:    +3.41 V  (min =  +1.04 V, max =  +1.70 V)  ALARM
5V:           +6.08 V  (min =  +0.22 V, max =  +2.72 V)  ALARM
CpuVcc:       +1.89 V  (min =  +0.06 V, max =  +0.43 V)  ALARM
1.5V:         +1.61 V  (min =  +0.46 V, max =  +0.82 V)  ALARM
3VSB:         +3.39 V  (min =  +0.10 V, max =  +3.36 V)  ALARM
VBAT:         +3.34 V  (min =  +0.88 V, max =  +0.06 V)  ALARM
FAN1:        8035 RPM  (min = 16875 RPM, div = 8)  ALARM
FAN3:        5625 RPM  (min =  815 RPM, div = 8)
FAN2:           0 RPM  (min = 1318 RPM, div = 128)  ALARM
FAN4:           0 RPM  (min = 1171 RPM, div = 128)  ALARM
CPU temp:     +42.0 C  (high = +136.0 C, hyst = +16.0 C)  ALARM  sensor = thermistor
System temp:  +24.5 C  (high = +80.0 C, hyst = +75.0 C)  sensor = thermistor


temp_buf=System temp:  +24.5 C  (high = +80.0 C, hyst = +75.0 C)  sensor = thermistor


tmp_buf=  +24.5 C  (high = +80.0 C, hyst = +75.0 C)  sensor = thermistor


val = 24

可以从sensors的运行结果中寻找到Sys的温度,传给变量val.

猜你喜欢

转载自blog.csdn.net/baidu_24553027/article/details/58168294