Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

reading fp-values written in Fortran D-format in C

6 views
Skip to first unread message

Daniel Franke

unread,
Feb 27, 2009, 7:53:35 AM2/27/09
to
[cross-posting to get views from both sides of the pond]

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.

Glen Herrmannsfeldt

unread,
Feb 27, 2009, 1:08:16 PM2/27/09
to
Daniel Franke wrote:

> 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

Daniel Franke

unread,
Mar 2, 2009, 6:10:44 AM3/2/09
to
Glen Herrmannsfeldt wrote:


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 ...

Dave Allured

unread,
Mar 4, 2009, 10:34:31 AM3/4/09
to
Daniel Franke wrote:
>
> [cross-posting to get views from both sides of the pond]
>
> 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?

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

0 new messages