%fscanf在C语言中如何使用%lf仅接受2位小数?我尝试使用%.2lf,但不起作用。

huangapple go评论86阅读模式
英文:

How does fscanf in c take in only 2 decimal places using %lf ? I tried %.2lf but it does not work

问题

我一直在尝试输入双精度值1200.00,而不是1200.00000000,这个值需要放到另一个文件中。但是我无法做到。

我尝试在我的代码中使用%.2lf,如下:

fscanf(file, "%.2lf", &items[i].cost);

但是我遇到了一个错误。

英文:

I've been trying to take in double value 1200.00 instead of 1200.00000000, this value needs to be put onto another file. But I'm unable to do so

I tried using %.2lf in my code like:

fscanf(file, "%.2lf", &items[i].cost);

But I am getting an error

答案1

得分: 2

The conversion specification "%.2lf" 不符合 fscanf() 的标准C或POSIX规范 — 点号不是可以出现在 fscanf() 转换规范中的字符之一:

> 每个转换规范由字符 '%' [CX] ⌦ 或字符序列 "%n$" ⌫ 引入,之后按顺序出现以下内容:
>
> * 一个可选的抑制赋值字符 '*'。
>
> * 一个可选的非零十进制整数,指定最大字段宽度。
> * [CX] ⌦ 一个可选的赋值分配字符 'm'。 ⌫
>
> * 一个指定接收对象大小的选项长度修饰符。
>
> * 一个指定应用的转换类型的转换规范字符。下面描述了有效的转换规范符。

你没有展示你检查了 fscanf() 调用是否成功 — 如果成功的话,它应该返回 1。读取没有受到字段宽度(最大字符数)的限制,除非你的库将 .2 解释为等同于 2(实际上忽略了点号)。没有办法将读取的数据限制为2位小数 — 除非输入数据是带有前导零的固定宽度,这样使用 %7lf 将跳过空格并读取数字的7个字符(1200.00),将其他内容留给下一个输入操作处理。请记住,%a%e%f%g 转换是可以互换的;它们都可以识别固定格式、指数格式或十六进制格式的浮点数输入字符串。

你也没有解释如何得到输出值 1200.00000000。通常这是因为你指定了 fprintf() 的转换规范,比如 %14.8f,它在小数点后打印8个字符。

所以:

  1. 检查 fscanf() 的返回值。
  2. 展示你如何打印数据。
英文:

The conversion specification "%.2lf" is not valid for fscanf() in either standard C or POSIX — the dot is not one of the characters that can appear in a fscanf() conversion specification:

> Each conversion specification is introduced by the character '%' [CX] ⌦ or the character sequence "%n$", ⌫ after which the following appear in sequence:
>
> * An optional assignment-suppressing character '*'.
>
> * An optional non-zero decimal integer that specifies the maximum field width.
> * [CX] ⌦ An optional assignment-allocation character 'm'. ⌫
>
> * An option length modifier that specifies the size of the receiving object.
>
> * A conversion specifier character that specifies the type of conversion to be applied. The valid conversion specifiers are described below.

You aren't showing that you checked that your call to fscanf() succeeded — it should return 1 if it does. The reading is not constrained by a field width (maximum number of characters to read), unless your library interprets the .2 as equivalent to 2 (effectively ignoring the dot). There isn't a way to limit the data that's read to 2 decimal places — unless the input data is a fixed width with leading zeros so that using %7lf will skip white space and read 7 characters for the number (1200.00), leaving anything else for the next input operation to process. Remember that the %a, %e, %f, and %g conversions are interchangeable; they all recognize fixed format, exponent format, or hexadecimal format floating-point input strings.

You've also not explained how you get the output value 1200.00000000. That would typically be because you specified a fprintf() conversion specification such as %14.8f, which prints 8 characters after the decimal point.

So:

  1. Check the return value from fscanf().
  2. Show how you're printing the data.

huangapple
  • 本文由 发表于 2023年4月20日 03:06:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定