0

I have a function

void getXFromFile3(FILE* fptr){
    double valueX;
    fscanf(fptr, "%lf\n", &valueX);
    printf("%lf", valueX);
}

and file data.dat with some double numbers.

One of them is -0.572869279, but my function print -0.572869. It looks like somewhere my number was cut off.

Any ideas what I am doing wrong?

mdolata
  • 184
  • 12
  • 3
    Try `printf("%.9lf", valueX);` (or more) because the default is 6 places. – Weather Vane May 20 '18 at 10:00
  • @WeatherVane But what if there are multiple `doubles` with different lengths after the decimal. We wouldn't want to print the extra zeros ( probably not the case here) and also what if it has greater than 9 digits say `x` digits? What then? – Rishav May 20 '18 at 10:03
  • @Rishav OP does not say why he is printing them - perhaps to check he read them properly. If OP wants to reproduce the file data exactly, read with `%s` and store that as well as processing with `sscanf`. The answer was: Nothing wrong, the number read is not being cut off. – Weather Vane May 20 '18 at 10:10
  • 1
    @Rishav if you want `x` places use `printf("%.*lf", x, valueX);` This is a non-question - it's all in the `printf` man page. – Weather Vane May 20 '18 at 10:12
  • @WeatherVane Aah aight, thanks. – Rishav May 20 '18 at 10:17
  • @WeatherVane : that file contains also number like `-0.22924787` which has 8 digits and printf add zeros. – mdolata May 20 '18 at 10:23
  • I was worried if my numbers are cut off, and that's just problem with printing – mdolata May 20 '18 at 10:25
  • The value might not match the input exactly anyway, please see [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) and [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Weather Vane May 20 '18 at 10:27
  • @WeatherVane: thanks for links, I already knew the problem with floating point – mdolata May 20 '18 at 10:39
  • Investigate whether the `%g` format with a precision of say 10 works for you when printing. The problem is in the printing, not the scanning. – Jonathan Leffler May 20 '18 at 11:01

1 Answers1

2

Tell scanf to tell you how many characters it scanned and then tell printf to print this number of digits after the comma.

Still one needs to take care of sign, and digits before the comma.

The code below assumes leading zeros and no positive signs were in the input.

void getXFromFile3(FILE* fptr){
  double x;
  int n;
  fscanf(fptr, "%lf%n", &x, &n);
  printf("%.*lf", /* the l length modifier is optional */
   n /* number of characters scanned */
   - (x < 0.) /* one off for the minus sign, if any */
   - ((int)log10(fabs(x)) + 1) /* off as many as digits before the comma */
   - 1, /* one off for the comma */
   x); 
}

Hu! ;-)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
alk
  • 69,737
  • 10
  • 105
  • 255
  • thanks! I don't need that to my program on studies I think, but still it is very interesting ;) – mdolata May 22 '18 at 07:54