我需要scanf
这种形式的数据:string whith spaces: 22.22kn 2.22L 。 (kn-kuna 是克罗地亚货币)
所以我需要将它保存在一个 string
和两个 double
变量中,我需要避免冒号、空格、kn 和 L。
我试过这个:
scanf(" %[^:] %lfL %lfkn\n",tmpName,&tmpQuant,&tmpPrice)
最佳答案
你几乎成功了。这是
// colon --+ +-----+---- the units were mixed up
// v v v
scanf(" %[^:]: %lfkn %lfL", tmpName, &tmpQuant, &tmpPrice);
但是,为了安全起见,您应该在字符串匹配中包含缓冲区的长度:
char tmpName[128]; // for example
// v--- length here
scanf(" %128[^:]: %lfkn %lfL", tmpName, &tmpQuant, &tmpPrice);
这样 scanf 就不会试图写入超出缓冲区的末尾。
关于c - 当你有字符串和双变量时如何扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27620233/