I need to read in data files which look like this:
* SZA: 10.00
2.648 2.648 2.648 2.648 2.648 2.648 2.648 2.649 2.650 2.650
2.652 2.653 2.652 2.653 2.654 2.654 2.654 2.654 2.654 2.654
2.654 2.654 2.654 2.655 2.656 2.656 2.657 2.657 2.657 2.656
2.656 2.655 2.655 2.653 2.653 2.653 2.654 2.658 2.669 2.669
2.667 2.666 2.666 2.664 2.663 2.663 2.663 2.662 2.663 2.663
2.663 2.663 2.663 2.663 2.662 2.660 2.656 2.657 2.657 2.657
2.654 2.653 2.652 2.651 2.648 2.647 2.646 2.642 2.641 2.637
2.636 2.636 2.634 2.635 2.635 2.635 2.635 2.634 2.633 2.633
2.633 2.634 2.634 2.635 2.637 2.638 2.637 2.639 2.640 2.640
2.639 2.640 2.640 2.639 2.639 2.638 2.640 2.640 2.638 2.639
2.638 2.638 2.638 2.638 2.637 2.637 2.637 2.634 2.635 2.636
2.637 2.639 2.641 2.641 2.643 2.643 2.643 2.642 2.643 2.642
2.641 2.642 2.642 2.643 2.645 2.645 2.645 2.645
What would be the most elegant way to read this file into an array of floats?
I know how to read each single line into a string, and I know how to convert the string to float using atof()
. But how do I do the rest the easiest?
I've heard about string buffers, might this help me?
The String Toolkit Library (Strtk) has the following solution to your problem:
More examples can be found in C++ String Toolkit (StrTk) Tokenizer.
Since this is tagged as C++, the most obvious way would be using streams. Off the top of my head, something like this might do:
Why
float
, BTW? Usually,double
is much better.Edit, since it was questioned whether returning a copy of the
vector
is a good idea:For a first solution, I'd certainly do the obvious. The function is reading a file into a
vector
, and the most obvious thing for a function to do is to return its result. Whether this results in a noticeable slowdown depends on a lot of things (the size of the vector, how often the function is called and from where, the speed of the disk this reads from, whether the compiler can apply RVO). I wouldn't want to spoil the obvious solution with an optimization, but if profiling indeed shows that this is to slow, the vector should be passed in per non-const reference.(Also note that C++1x with rvalue support, hopefully soon to be available by means of a compiler near you, will render this discussion moot, as it will prevent the vector from being copied upon returning from the function.)
Simple solution using STL algorithms:
If you really need to use an array, you must first allocate it (either dynamically or statically if you know the size) and then you can use the same copy algorithm
But I would recommend using std::vector even in this case, if you need to pass the data into a function that takes an array you can always pass it as a pointer to the first element:
I would do something like this:
NOTE:
header_is_correct(header)
is just an example, you will need to implement any error checking for that first line manually there.