Force printf format?

H

hamsya

Hello world :)

I have some ASCII files with a number of float values inside. These
files are created by a Fortran code which I can not modify.

On the other hand, I am working on a C application which is supposed
to output the same data.

My dream is to run any diff program on these files to validate my C
application against the Fortran one, but for now it seems impossible.

Values issued from Fortran are stored as "0.1000000E+01".
The C printf("%14.7E", value) gives "1.0000000E+00".

Is there a way to control printf in order to have the same
representation in both cases?

Crea.
 
C

Charlie Gordon

Hello world :)

I have some ASCII files with a number of float values inside. These
files are created by a Fortran code which I can not modify.

On the other hand, I am working on a C application which is supposed
to output the same data.

My dream is to run any diff program on these files to validate my C
application against the Fortran one, but for now it seems impossible.

Values issued from Fortran are stored as "0.1000000E+01".
The C printf("%14.7E", value) gives "1.0000000E+00".

Is there a way to control printf in order to have the same
representation in both cases?

Not that I know, but you could write a utility to compare the files by
parsing both with fscanf and comparing the numbers. If you are lucky, the
difference you mention should not show in the resulting numbers.
 
H

hamsya

Not that I know, but you could write a utility to compare the files by
parsing both with fscanf and comparing the numbers.

Yes I could but I really don't want to do this :) There are several
types of data files to compare, and I'll have to write a separate
parser for each file format.

Anyway thanks for your answer, Charlie :D
 
C

Charlie Gordon

Yes I could but I really don't want to do this :) There are several
types of data files to compare, and I'll have to write a separate
parser for each file format.

Anyway thanks for your answer, Charlie :D

In this case, you might want to write a utility function to format your
numbers appropriately by post processing the output of snprintf:

void post_process(char *p) {
/* p points to a buffer holding the converted value as produced by
sprintf */
/* this buffer is assumed to be large enough for one extra digit */
int exp;
if (*p == '+' || *p == ' ' || *p == '-')
number++;
if (isdigit((unsigned char)*p) && p[1] == '.' && strchr(p, 'E')) {
p[1] = *p;
*p = '.';
p = strchr(p, 'E') + 1;
if (*p == '+') {
p += 1;
sprintf(p, "%d", atoi(p) + 1);
}
}

This is a general idea, you may have to customize this function to produce
the desired format for specific values of the exponent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top