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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器