我希望在用sscanf处理字符串时忽略特定字段。对于sscanf的如何用sscanf忽略字段(%*被拒绝)
手册页说
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().
试图在Golang使用这个,忽略第三场:
if c, err := fmt.Sscanf(str, " %s %d %*d %d ", &iface.Name, &iface.BTx, &iface.BytesRx); err != nil || c != 3 {
编译OK,但在运行时ERR为:
bad verb %* for integer
Golang DOCO没有具体提及%*转换规范,但它说,
Package fmt implements formatted I/O with functions analogous to C's printf and scanf.
这并不表明%*不落实,所以...我是不是做错了?或者它刚刚被安静地省略了? ...但是,为什么要编译?
感谢您的回复。 我还没有完全遵循。要清楚说明我的意图 - 我希望放弃输入的第三个字段。 即,禁止转换为参数。 您的代码行似乎提供了将该字段转换为参数的宽度精度。 – maxfacta 2013-04-05 03:52:37
哦,似乎我误解了你的问题,我会更新我的答案。 – nemo 2013-04-05 04:01:12
干杯......“不能做”不是我想听到的答案,但它仍然是一个答案! 我在这里的例子被修剪以便于查看 - 我实际上是解析/ proc/net/dev的输出,并且只对字段1,2和10感兴趣。我不打算声明7个不需要的int ...我有一个使用字符串的多线解决方案。 Split,Fields,Trim和strconv.Atoi - 在C中,我会使用单个sscanf。 – maxfacta 2013-04-05 04:54:28