Re: ifstream question
In article <3t6oj.21$az7.5@newsfe07.lga>, jgbrawley@charter.net says...
[ ... ]
BUT: O, beautiful synergy: as it happens, the first file line's three
numbers (the rest have five) are not intended to go into my x,y,z,r,c
database, so all I had to do was read these three and their two char type
commas (but _not_ read the newline) _outside_ of the loop, which then gave
the loop exactly what it expected (a char first --the \n-- in the loop-read)
when I went inside the loop to fill the database with numbers.
IOW, it was merely a matter of which, a number or a character, I read first
in the loop. If the number first, it missed the last number, and if the
char first, it missed the whole file. (I could also have forced my
file_writer_ to stick a comma into position 0 in the file, but that'd have
been a major kludge; ugly and obviously a very clumsy workaround).
Thanks for the more info, Andy (and Alf), but this seems to have been a case
of a neophyte (me) doing something _so_ obvious and stupid that the experts
here (you included) didn't see what sheer idiocy I had perpetrated upon
myself.
(*grin*)
I wouldn't be quite so hard on yourself -- things like this really can
be a pain for almost anybody to get right.
There are other ways that can be a bit easier though. For one example,
when you read numbers from a stream, the stream uses a locale to
actually read the numbers, as well as to classify the other characters
that get read from the stream. When you read numbers, it treats white-
space as delimiters between the numbers -- e.g. if you started with a
file like:
1 2 3 4 5
instead of:
1,2,3,4,5
it would have skipped the white-space automatically, and just read
numbers.
That leads to a fairly simple (if somewhat oddball) solution: create a
locale that says ',' is a whitespace character, and then tell the stream
to use that locale:
class my_ctype : public std::ctype<char> {
mask my_table[UCHAR_MAX];
public:
my_ctype(size_t refs = 0)
: std::ctype<char>(my_table, false, refs)
{
// copy the normal character class table, so ' ', tab, etc., are still
// white space
std::copy(classic_table(),
classic_table() + table_size,
my_table);
// for our purposes, comma is also white space.
my_table[','] = (mask)space;
}
};
int add(int a, int b) { return a+b; }
int main() {
// create a locale that includes our ctype:
std::locale x(std::locale::classic(), new my_ctype);
// and tell cin to use the new locale:
std::cin.imbue(x);
// now we'll read numbers from standard input into a vector
std::vector<int> numbers;
std::copy(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::back_inserter(numbers));
// print out the numbers:
std::copy(numbers.begin(), numbers.end(),
std::ostream_iterator<int>(std::cout, "\n"));
std::cout << "\n";
// add numbers to show they really are numbers:
std::cout << std::accumulate(numbers.begin(), numbers.end(),
0, add);
return 0;
}
input:
1,2,3,4,5
output:
1
2
3
4
5
15
--
Later,
Jerry.
The universe is a figment of its own imagination.