我希望在用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