dongni8124 2013-05-14 07:26
浏览 232
已采纳

fscanf()/ sscanf() - 匹配变长的空格?

I would to use sscanf() or (preferably) fscanf() to scan /proc/meminfo and return MemTotal:

MemTotal:        1027156 kB
MemFree:          774596 kB
Buffers:           23816 kB
Cached:           198300 kB

Since the number of spaces between MemTotal: and the actual number varies, I'm wondering if sscanf() is smart enough to parse something like:

sscanf($line, "MemTotal:\s+%d");

Will the padding and alignment specifiers of sprintf() work with sscanf() as well?

I would try this out on my own, but I don't have a dev / Linux environment available ATM.

  • 写回答

2条回答 默认 最新

  • duanruoyu6675 2013-05-14 07:58
    关注

    Yes, sscanf is smart enough to parse that with an arbitrary number of spaces between. You don't even need to specify it in any special way. Just do

    sscanf($line, '%s%d');
    

    and you'll get

    Array
    (
        [0] => MemTotal:
        [1] => 1027156
    )
    

    Unfortunately, the PHP Manual isn't too complete about what you can use as the format string, but since it's based on the UNIX command, you can look at other documentations to find what options you have:

    It's a bit fiddly to get them working though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示