Generic said:
I am trying to set up a delimiter character string for the input stream
such that the delimiter string is "skipped over" in the input stream.
Can someone suggest how to do this with some sample code? I am
attaching a "C" code snippet that does the same thing. The problem is
that I don't know how to realize this using "cin"
Thanks,
Lee
The correct way to handle this is to define an input stream manipulator
that accepts the "delimiter string" as an argument. This manipulator
should intelligently handle optional delimiter characters. If the
manipulator encounters the "delimiter string", it steps over it. If
not, it stores the value read into a "shared buffer", as described in
K&R Section 4.3. During reading, attempt is made to first read from
the "shared buffer" and then the input stream. Of course, this
requires changing the way the extraction operator works, which will not
happen unless this idea makes it to the standard.
We all know that manipulators that accept arguments are a little tricky
(The C++ Programming Language, Bjarne Stroustrup, 3rd Edition, Section
21.4.6.1) The way it is done is to define a class/structure that has
the overloaded operator >> friend function. This overloaded function
accepts the reference to the input stream and the reference to an
object of the aforementioned class/structure as arguments, and returns
the reference to the input stream. Here's an example:
struct skipDelimiter
{
const char* delimPattern;
skipDelimiter(const char* str): delimPattern(str){}
friend istream& operator>>(istream&, const skipDelimiter&);
};
istream& operator>>(istream& istrm, const skipDelimiter& sd)
{
// IF delimiter pattern found
// THEN
// skip over it
// ELSE
// push the pattern found into a shared buffer
// (as described in K&R Section 4.3)
// ENDIF
// return
return istrm;
}
For completeness, here's a code snippet for a manipulator that does not
take arguments. All that is needed here is a function that accepts the
reference to the input stream as an argument, and returns the reference
to the input stream.
istream& printdate(istream& istrm)
{
// Meaningless example, for illustrative
// purposes only
time_t lt;
time(<);
cout << ctime(<);
return istrm;
}
Regards,
Nimmi