Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse file containing hexadecimals in the form -0x1.0c7474fp+8 in c++?

I am trying to parse a file data.txt containing hexadecimals of the form -0x1.0c7474fp+8:

-0x1.e51b67p+0 0x1.0b18ef5p+8 0x1.9c6075p+8 -0x1.190d44p+4
-0x1.e4f7838p+0 0x1.0b6446dp+8 0x1.9d779b8p+8 -0x1.16436ap+3
-0x1.e4555bp+0 0x1.0b6d446p+8 0x1.9e28c4ap+8 -0x1.1f8b0ep+3
-0x1.e3b9598p+0 0x1.0b7982cp+8 0x1.9edf5f8p+8 -0x1.f80bc6p+2
-0x1.e2f5896p+0 0x1.0b70bd7p+8 0x1.9f75c6ap+8 -0x1.07390ep+3
-0x1.e21d2dep+0 0x1.0b5961cp+8 0x1.9fef6e6p+8 -0x1.031012p+3
-0x1.e225b96p+0 0x1.0bbc104p+8 0x1.a12c75cp+8 -0x1.06951cp+3
-0x1.e17ec4cp+0 0x1.0bc35b7p+8 0x1.a1de5a6p+8 -0x1.25f138p+3
-0x1.e0e7f36p+0 0x1.0c140edp+8 0x1.a300c22p+8 -0x1.417644p+3
-0x1.e0e7e0ap+0 0x1.0c7474fp+8 0x1.a43f084p+8 -0x1.359f22p+3
-0x1.e076e6cp+0 0x1.0c93aebp+8 0x1.a50efaap+8 -0x1.00e406p+4
-0x1.e1c339cp+0 0x1.0ec7d62p+8 0x1.ab2777p+8 -0x1.1b6134p+3
-0x1.e0614eap+0 0x1.0e669d1p+8 0x1.ab301b8p+8 -0x1.6a997cp+2
-0x1.e01577p+0 0x1.0e9f2e5p+8 0x1.ac33c36p+8 -0x1.62eb42p+2
-0x1.de237dcp+0 0x1.0df5506p+8 0x1.abd2c42p+8 -0x1.8951c2p+2

I write this c++ code without any success:

#include <iostream>
#include <fstream>

int main()
{
    float a, b, c, d;

    std::ifstream ifs("data.txt");

    int n = 0;
    while(ifs >> std::hex >> a
              >> std::hex >> b
              >> std::hex >> c
              >> std::hex >> d)
    {
        std::cout << a << " "
                  << b << " "
                  << c << " "
                  << d << std::endl;
        ++n;
    }
    std::cout << "n=" << n << std::endl;

    return 0;
}

Edit: I tried with std::hexfloat instead but it's still not working with gcc as shown in this example.

like image 480
T.L Avatar asked Sep 17 '18 18:09

T.L


2 Answers

The -0x1.e51b67p+0 gobble-de-gook is the output of std::hexfloat, an addition to C++'s Standard Library in the C++ 11 Standard Revision.

If you replace

while(ifs >> std::hex >> a
          >> std::hex >> b
          >> std::hex >> c
          >> std::hex >> d)

with

while(ifs >> std::hexfloat >> a
          >> std::hexfloat >> b
          >> std::hexfloat >> c
          >> std::hexfloat >> d)

and your compiler supports C++11 and hexfloat (G++ was a bit laggy in this) you should be good to go.

Edit: G++ was laggy in including std::hexfloat in the headers. Unfortunately as of today, GCC 8.2, support for std::hexfloat is incomplete and not working. This answer does not work for the Asker.

Edit 2: I deserve to fall on my sword for this, but fortunately I don't have a sword. In my obsessive digging into why this didn't work in C++ and trying to find a polite way to make this work in C++, I completely forgot the fact that this works just fine in C.

Since std::hexfloat is held up while the wording of the C++ Standard is sorted out, let's follow the Git-R Done! Principle, use C, and wrap it up in a function so it can easily be replaced with idiomatic C++ later.

#include <cstdio>
#include <iostream>

int main()
{
    float a, b, c, d;

    FILE *f = fopen("data.txt", "r");

    if (f == NULL)
    {
        std::cerr << "Unable to open file."
        return -1;
    }
    int n = 0;
    while(fscanf(f, "%a %a %a %a", &a, &b, &c, &d) == 4)
    {
        std::cout << a << " "
                  << b << " "
                  << c << " "
                  << d << std::endl;
        ++n;
    }
    std::cout << "n=" << n << std::endl;

    return 0;
}
like image 54
user4581301 Avatar answered Nov 19 '22 22:11

user4581301


seems GCC is still not able to handle "hexFloat" properly, check out gcc bug tracker:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59987

How about this simple alternative solution?

#include <iostream>

int main() {
    const char *data = "-0x1.e51b67p+0 0x1.0b18ef5p+8 0x1.9c6075p+8 -0x1.190d44p+4 -0x1.e4f7838p+0 0x1.0b6446dp+8 0x1.9d779b8p+8 -0x1.16436ap+3";

    char *end;
    for (float f = strtof(data, &end); data != end; f = strtof(data, &end))
    {
        data = end;
        if (errno == ERANGE){
            printf("range error, got ");
            errno = 0;
        }
        printf("%a\n", f);
    }

    return 0;
}

https://wandbox.org/permlink/69MpeNKexPWaOiZc

like image 24
Beastmaster Avatar answered Nov 20 '22 00:11

Beastmaster