douweiduo7526 2013-04-04 18:40
浏览 366
已采纳

如何使用sscanf忽略字段(%*被拒绝)

I wish to ignore a particular field whilst processing a string with sscanf.

Man page for sscanf says

An optional '*' assignment-suppression character: scanf() reads input as directed by the conversion specification, but discards the input. No corresponding pointer argument is required, and this specification is not included in the count of successful assignments returned by scanf().

Attempting to use this in Golang, to ignore the 3rd field:

if c, err := fmt.Sscanf(str, " %s %d %*d %d ", &iface.Name, &iface.BTx, &iface.BytesRx); err != nil || c != 3 {

compiles OK, but at runtime err is set to:

bad verb %* for integer

Golang doco doesn't specifically mention the %* conversion specification, but it does say,

Package fmt implements formatted I/O with functions analogous to C's printf and scanf.

It doesn't indicate that %* is not implemented, so... Am I doing it wrong? Or has it just been quietly omitted? ...but then, why does it compile?

  • 写回答

1条回答 默认 最新

  • dongwenhui8900 2013-04-04 18:57
    关注

    To the best of my knowledge there is no such verb (as the format specifiers are called in the fmt package) for this task. What you can do however, is specifying some verb and ignoring its value. This is not particularly memory friendly, though. Ideally this would work:

    fmt.Scan(&a, _, &b)
    

    Sadly, it doesn't. So your next best option would be to declare the variables and ignore the one you don't want:

    var a,b,c int
    fmt.Scanf("%d %v %d", &a, &b, &c)
    fmt.Println(a,c)
    

    %v would read a space separated token. Depending on what you're scanning on, you may fast forward the stream to the position you need to scan on. See this answer for details on seeking in buffers. If you're using stdio or you don't know which length your input may have, you seem to be out of luck here.

    It doesn't indicate that %* is not implemented, so... Am I doing it wrong? Or has it just been quietly omitted? ...but then, why does it compile?

    It compiles because for the compiler a format string is just a string like any other. The content of that string is evaluated at run time by functions of the fmt package. Some C compilers may check format strings for correctness, but this is a feature, not the norm. With go, the go vet command will try to warn you about format string errors with mismatched arguments.

    Edit:

    For the special case of needing to parse a row of integers and just caring for some of them, you can use fmt.Scan in combination with a slice of integers. The following example reads 3 integers from stdin and stores them in the slice named vals:

    ints := make([]interface{}, 3)
    vals := make([]int, len(ints))
    
    for i, _ := range ints {
        ints[i] = interface{}(&vals[i])
    }
    
    fmt.Scan(ints...)
    fmt.Println(vals)
    

    This is probably shorter than the conventional split/trim/strconv chain. It makes a slice of pointers which each points to a value in vals. fmt.Scan then fills these pointers. With this you can even ignore most of the values by assigning the same pointer over and over for the values you don't want:

    ignored := 0
    
    for i, _ := range ints {
        if(i == 0 || i == 2) {
            ints[i] = interface{}(&vals[i])
        } else {
            ints[i] = interface{}(&ignored)
        }
    }
    

    The example above would assign the address of ignore to all values except the first and the second, thus effectively ignoring them by overwriting.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 FPGA芯片60进制计数器
  • ¥15 前端js怎么实现word的.doc后缀文件在线预览
  • ¥20 macmin m 4连接iPad
  • ¥15 DBIF_REPO_SQL_ERROR
  • ¥15 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部