Re: inconsistencies when compiling
On Jan 26, 3:44 am, Jerry Coffin <jcof...@taeus.com> wrote:
In article <05a2f2d6-a2a5-4a32-9830-
faa22aac7...@s8g2000prg.googlegroups.com>, james.ka...@gmail.com says...
[ ... ]
I'm really thinking that someone should write up a beginner's
guide to using iostream, with the standard idioms, and post it
somewhere so we could point to it.
Let's see. Reading input:
std::copy(std::istream_iterator<some_type>(instream),
std::istream_iterator<some_type>(),
std::back_inserter(your_collection));
Writing output:
std::copy(your_collection.begin(), your_collection.end(),
std::ostream_iterator<some_type>(outstream, "\t"));
Covers an amazing percentage in two statements... :-)
Do you know, I rarely use either. Once in a blue moon, I'll use
the first to file an std::vector< Line >, or some such, with the
operator for Line which does all the work, and I'll
occasionally use the second for a small set of elements. But
both cases are exceptional. For most simple cases, the input
is:
while ( std::getline( in, line ) ) {
++ lineNo ;
boost::smatch fields ;
if ( ! boost::regex_match( line, fields, syntax ) ) {
// error...
} else {
// process fields...
}
}
or:
while ( std::getline( in, line ) ) {
std::istringstream data( line ) ;
data >> field1 ... >> fieldLast >> std::ws ;
if ( ! data || data.get() != EOF ) {
// error...
} else {
// process fields ...
}
}
Often, I'll use a mixture, using the regular expression for
syntax checking, but an istringstream for extracting the actual
data. (The istringstream will convert numeric values, for
example, which the regular expression won't. And my pre-Boost
regular expression class didn't handle \(...\) either, so I
couldn't use it to collect the fields.) I've also got a number
of ready made classes for breaking a line up into fields
according to "standard" rules, so I frequently write something
like:
while ( std::getline( in, line ) ) {
Gabi::SomeFieldArray fields( line ) ;
// here, fields is nothing more than an std::vector,
// with fields[ 0 ] == line, fields[ 1 ] with the
// first field, etc.
// As you can see, I'm very used to AWK:-).
}
In practice, those are about the only cases I use anything other
than character input (with lex/flex). (In any of them, of
course, if the syntax leads me to expect a table, I might set up
a filtering streambuf to return EOF at the end of the table, and
then use std::copy to read the table. But that doesn't seem to
happen very often in my own code.)
Output is simpler, but of course, no where have we begun to talk
about error detection and handling.
But I was actually suggesting a description at a slightly lower
level, the basic loop, for example, but not what you do in it.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34