Re: Reading an array from file?
In article <4a784940$0$303$14726298@news.sunsite.dk>, fdm@gk.com
says...
Hi I have a .txt file containing:
[-0.00231844, -0.02326, 0.0484723, 0.0782189, 0.0917853,
[ ... ]
I
still need to "tokenize" this string into its double parts. Before throwing
myself into cumbersome code I would like to hear if anyone has a good idea
to do this the right way.
I'm not sure if it's really "right" (some would argue that it's
downright wrong), but for your purposes, the commas are essentially
the same as white space -- i.e. they're something between the data
that you ignore. As such, I'd probably just create a locale where
',' is treated as white space, and then let the iostream handle the
"parsing" involved:
#include <iostream>
#include <locale>
#include <algorithm>
#include <vector>
struct digits_only: std::ctype<char>
{
digits_only(): std::ctype<char>(get_table()) {}
static std::ctype_base::mask const* get_table()
{
static std::ctype_base::mask
rc[std::ctype<char>::table_size];
static bool inited = false;
if (!inited)
{
// Everything is a "space"
std::fill_n(rc, std::ctype<char>::table_size,
std::ctype_base::space);
// unless it can be part of a floating point number.
std::fill_n(rc+'0', 9, std::ctype_base::mask());
rc['.'] = std::ctype_base::mask();
rc['-'] = std::ctype_base::mask();
rc['+'] = std::ctype_base::mask();
rc['e'] = std::ctype_base::mask();
rc['E'] = std::ctype_base::mask();
inited = true;
}
return rc;
}
};
int main() {
int low, high;
std::cin.imbue(std::locale(std::locale(), new digits_only()));
std::vector<double> data;
std::copy(std::istream_iterator<double>(std::cin),
std::istream_iterator<double>(),
std::back_inserter(data));
std::copy(data.begin(), data.end(),
std::ostream_iterator<double>(std::cout, "\n"));
return 0;
}
This should accept any valid input. Most invalid data in the input
file will simply be ignored. A few things in the input could stop the
input at that point though. Just for example, input that contained
text would fail at the first 'e' or 'E' in the text -- those can't be
ignored because they _could_ be part of a f.p. number, but by itself
(without some digits), the attempt to convert it as an f.p. number
would fail.
--
Later,
Jerry.