Problem with ios::flags

P

Paulo da Silva

Hi!
Please consider the following fragment

std::cin.exceptions(std::ios::failbit);
....
try
{ is.getline(p,ILEN);
...
}
catch (std::ios::failure const &problem)
{ if (WHAT HERE?)
{ // '\n' not found yet
}
else
{ // Real failure
}
}

How do I tell a real failure from just ILEN-2 chars already read but not
yet '\n'?

Thanks for any help.
 
J

John Harrison

Paulo said:
Hi!
Please consider the following fragment

std::cin.exceptions(std::ios::failbit);
...
try
{ is.getline(p,ILEN);
...
}
catch (std::ios::failure const &problem)
{ if (WHAT HERE?)
{ // '\n' not found yet
}
else
{ // Real failure
}
}

How do I tell a real failure from just ILEN-2 chars already read but not
yet '\n'?

Thanks for any help.

You mean ILEN-1 chars. If the fail bit is set there will either be
ILEN-1 chars or zero chars (not counting the null terminator) in p. So
you can test for that.

if (strlen(p) == ILEN-1)
{
}
else
{
}

Here's a good site that explains exactly what istream::getline does.

http://www.dinkumware.com/manuals/d...leat&page=istream.html#basic_istream::getline

It you haven't considered it already you should defintely think about
switching to the string version of getline

http://www.dinkumware.com/manuals/default.aspx?manual=compleat&page=string2.html#getline

Might be no need to treat long lines as a special case then.

john
 
P

Paulo da Silva

John Harrison escreveu:
....


You mean ILEN-1 chars
Of course ... I mistyped 2 ...
.. If the fail bit is set there will either be
ILEN-1 chars or zero chars (not counting the null terminator) in p. So
you can test for that.

if (strlen(p) == ILEN-1)
{
}
else
{
}
Thanks. Anyway I thought of a flags bit or something and was not sure if
strlen - zero terminated (or gcount()) is implementation independent
for this purpose.
Here's a good site that explains exactly what istream::getline does.

http://www.dinkumware.com/manuals/d...leat&page=istream.html#basic_istream::getline
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,318
Latest member
LuisWestma

Latest Threads

Top