Force printf format?

Discussion in 'C Programming' started by hamsya@yahoo.fr, Oct 22, 2007.

  1. Guest

    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.
     
    , Oct 22, 2007
    #1
    1. Advertising

  2. <> a ecrit dans le message de news:
    ...
    > 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.

    --
    Chqrlie.
     
    Charlie Gordon, Oct 22, 2007
    #2
    1. Advertising

  3. Guest

    > 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
     
    , Oct 23, 2007
    #3
  4. <> a écrit dans le message de news:
    ...
    >> 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


    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.

    --
    Chqrlie.
     
    Charlie Gordon, Oct 23, 2007
    #4
  5. Guest

    Great! I'll implement something like this, thank you for the idea :)
     
    , Oct 24, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chu
    Replies:
    3
    Views:
    4,223
    Shawn
    Aug 15, 2006
  2. ben
    Replies:
    4
    Views:
    647
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,309
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    452
  5. guru
    Replies:
    8
    Views:
    299
Loading...

Share This Page