tags:

views:

66

answers:

2

Okay, so i have a fairly annoying problem, one of the applications we use hdp, dumps HDF values to a text file.

So basically we have a text file consisting of this:

-8684 -8683 -8681 -8680 -8678 -8676 -8674 -8672 -8670 -8668 -8666 
-8664 -8662 -8660 -8657 -8655 -8653 -8650 <trim... 62,000 more rows>

Each of these represent a double: E.g.:

-8684 = -86.84

We know the values will be between 180 -> -180. But we also have to process around 65,000 rows of this. So time is kinda important.

Whats the best way to deal with this? (i can't use Boost or any of the other libraries, due to internal standards)

+4  A: 

As you wish, as an answer instead... :)

Can't you just use standard iostream?

double val; cin >> &val; val/=100;

rinse, repeat 62000*11 times

roe
A: 

I think I'd do the job a bit differently. I'd create a small sorta-proxy class to handle reading a value and converting it to a double:

class fixed_point { 
    double val;
public:
    std::istream &read(std::istream &is) { 
        is >> val; val /= 100.0; return is;
    }
    operator double() { return val; }
    friend std::istream &operator>>(std::istream &is, fixed_point &f) {
        return f.read(is);
    }
};

Using that, I could read my data a bit more cleanly. For example:

std::vector<double> my_data;
std::copy(std::istream_iterator<fixed_point>(infile), 
          std::istream_iterator<fixed_point>(),
          std::back_inserter(my_data));

The important point here is that operator>> doesn't just read raw data, but extracts real information (in a format you can use) from that raw data.

There are other ways to do the job as well. For example, you could also create a derivative of std::num_get that parses doubles from that file's format. This is probably the way that's theoretically correct, but the documentation for this part of the library is mostly pretty poor, so I have a hard time advising it.

Jerry Coffin