类scanf函数中%[*]type的巧用场景

2011-02-24 wcdj

 

%[*]type,此问题对scanf和fscanf通用。关于*号用法的解释如下。
%[*][width][modifiers]type
* —— An optional starting asterisk indicates that the data is to be retrieved from stdin but ignored, i.e. it is not stored in the corresponding argument.

应用场景1:
将文件中的数据读入到指定的变量中,文件中数据的格式为:(每行第一个数为行号,后三个数为int型,再后三个数为double型)
1 1,2,3,1.2,3.4,5.5
2 22,31,100,1.0,2.2,-3.5
……

 

 

结论:使用%*d可以跳过第一列的序号。

应用场景2:
从文件中逐个读出能读的浮点数,比如"1.0,3.5,2.2 ……"
题目来源:
http://topic.csdn.net/u/20110218/12/E039E736-CA2F-4168-B06A-14D386D511C5.html
http://topic.csdn.net/u/20110223/15/3fd75e15-fe7d-432b-8b6c-c048ddec20a0.html?38465

 

测试数据:
1 1,2,3,1.2,3.4,5.5
2 22,31,100,1.0,2.2,-3.5
wcdj 2011 hello123.456world #123&456*789!

输出:
[1]==1
[2]==1
[3]==2
[4]==3
[5]==1.2
[6]==3.4
[7]==5.5
[8]==2
[9]==22
[10]==31
[11]==100
[12]==1
[13]==2.2
[14]==-3.5
[15]==2011
[16]==123.456
[17]==123
[18]==456
[19]==789

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值