Chad E. Dollins wrote:
My question is how can I use iostream to tell the diffrence between a read
on an empty line and the other.
You simple need to process the whitespaces yourself rather than
leaving it to the formatted extractor. To do so, you need to
understand that formatted extractors begin processing by skipping
whitespaces. That is, after reading anything, the following
whitespaces are not touched and you can simple read them yourself.
Here is a possible approach how to do this conveniently:
#include <iostream>
#include <ctype.h>
#include <stdexcept>
struct nlcount { int count; nlcount(): count(0) {} };
std::istream& operator>> (std::istream& in, nlcount& nls)
{
std::istream::sentry kerberos(in, true);
if (kerberos)
{
std::streambuf* sbuf = in.rdbuf();
int eof = std::char_traits<char>::eof();
nls.count = 0;
int c = sbuf->sgetc();
for (; c != eof && std::isspace(c); c = sbuf->snextc())
if (c == std::char_traits<char>::to_int_type('\n'))
++nls.count;
if (c == eof)
in.setstate(std::ios_base::eofbit);
}
return in;
}
void read(std::istream& in)
{
int blocks = 0;
nlcount nls;
if (!(in >> blocks >> nls) || nls.count != 2)
throw std::runtime_error("missing newline after block count");
int value = 0;
for (int count = 1; count <= blocks && in; )
if (in >> value >> nls)
{
std::cout << count << ": " << value << "\n";
if (nls.count == 2)
++count;
}
}
int main()
{
try { read(std::cin); }
catch (std::exception const& ex) {
std::cerr << "ERROR: " << ex.what() << "\n";
}
}
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence