Hi all.
I have some trouble reading floating-point numbers written with the Fortran
D-descriptor:
$> cat dout.f90
write( *, FMT="(d13.5)") 3.1415
end
$> gfortran dout.f90 -o dout && ./dout
0.31415D+01
Reading above number via [f]scanf in C:
$> cat din.c
#include <stdio.h>
int main() {
double x;
fscanf(stdin, "%lf", &x);
fprintf(stdout, "%lf\n", x);
return 0;
}
$> gcc din.c -o din && ./dout | din
0.314150
While I can update the application that writes files in above format, I
still have to deal with existing files. Besides converting these files via
sed, is there anything in C (short of reimplementing/patching scanf) that
would allow me to read numbers in this format properly?
Thanks
Daniel
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
> I have some trouble reading floating-point numbers written
> with the Fortran D-descriptor:
(snip)
> While I can update the application that writes files in above format, I
> still have to deal with existing files. Besides converting these files via
> sed, is there anything in C (short of reimplementing/patching scanf) that
> would allow me to read numbers in this format properly?
Similar to the suggestion often given for Fortran I/O, read the line
into a buffer with fgets, convert D to E, possibly with a STRCHR loop,
and then use sscanf to convert it.
That works pretty well if the file has a line (record) structure,
slightly less well if it doesn't.
-- glen
Glen,
thanks for your suggestion. Unfortunately, in my case it may work slightly
less well. Still worth to give a try :)
Regards
Daniel
P.S. Although you replied five days ago, I only got the message today.
Strange ...
Just read each input line as a string, change the D's to e's in the
string, then read the edited numbers from the string instead of with
fscanf. I do not know what the C function is to read floats from a
string; I am responding from the fortran group. I figure that should be
simple enough in C, though. HTH.
--Dave