istringstream behaviour when no whitespace present in input

D

Diwa

Does istringstream require one whitespace at a min ?
In the code below I expected "first" and "secondline" to be printed.
Only "first" got printed.

The code is as follows:
// -----------------------------------------------------------------
#include <iostream> // std::cout
#include <fstream> // ifstream
#include <sstream> // stringstream
#include <string>

int main( )
{
std::string cfgSvrName;
std::istringstream iss_1("first line");
std::istringstream iss_2("secondline");

iss_1 >> cfgSvrName;
if (iss_1.good( ))
std::cout << "cfgSvrName - ******" << cfgSvrName << "******\n";

iss_2 >> cfgSvrName;
if (iss_2.good( ))
std::cout << "cfgSvrName - ******" << cfgSvrName << "******\n";
}
// -----------------------------------------------------------------


Executing it gives
// -----------------------------------------------------------------
$ ./a.out
cfgSvrName - ******first******
// -----------------------------------------------------------------

Thanks
Diwakar
 
H

Howard Hinnant

"Diwa said:
Does istringstream require one whitespace at a min ?
In the code below I expected "first" and "secondline" to be printed.
Only "first" got printed.

The code is as follows:
// -----------------------------------------------------------------
#include <iostream> // std::cout
#include <fstream> // ifstream
#include <sstream> // stringstream
#include <string>

int main( )
{
std::string cfgSvrName;
std::istringstream iss_1("first line");
std::istringstream iss_2("secondline");

iss_1 >> cfgSvrName;
if (iss_1.good( ))
std::cout << "cfgSvrName - ******" << cfgSvrName << "******\n";

iss_2 >> cfgSvrName;
if (iss_2.good( ))
std::cout << "cfgSvrName - ******" << cfgSvrName << "******\n";
}
// -----------------------------------------------------------------


Executing it gives
// -----------------------------------------------------------------
$ ./a.out
cfgSvrName - ******first******
// -----------------------------------------------------------------

good() tests eofbit as well. iss_2 had a successful read but also set
eofbit. Try testing !fail() instead. I believe that more closely
matches your intent.

-Howard
 
D

Diwa

Howard said:
good() tests eofbit as well. iss_2 had a successful read but also set
eofbit. Try testing !fail() instead. I believe that more closely
matches your intent.

Bingo Howard, it worked.

I tried to use "good( ) || eof( )" instead of "!fail( )"
but when I tried to read after EOF, "eof( )" is still
set. So "good( ) || eof( )" was indicating all was
okay even though it was not the case. So "!fail( )"
is right solution to use.

Btw, don't you think it should be "!fail( ) && !bad( )"

Thanks
Diwakar
 
H

Howard Hinnant

"Diwa said:
Btw, don't you think it should be "!fail( ) && !bad( )"

That doesn't hurt, but it would be redundant. From 27.4.4.3p9 it says
that fail() checks both failbit and badbit.

If it only checked failbit, that would be just what people expected and
thus not nearly as fun for the standards committee. Just kidding. :)
There's a footnote that says:
Checking badbit also for fail() is historical practice.

And in practice it is usually what everyone wants anyway.

In summary, there's approximately two useful calls in this area (imho):

Check good() before the input if you want to know if it is possible the
input might succeed.

Check fail() after the input if you want to know if the input did
succeed.

Imho the second check is the more useful in practice, but I sometimes
use the first too (e.g. to avoid some computation prior to input).

-Howard
 
D

Diwa

Howard said:
And in practice it is usually what everyone wants anyway.

In summary, there's approximately two useful calls in this area (imho):

Check good() before the input if you want to know if it is possible the
input might succeed.

Check fail() after the input if you want to know if the input did
succeed.

Imho the second check is the more useful in practice, but I sometimes
use the first too (e.g. to avoid some computation prior to input).
Useful and easy to remember summary...thanks....
 
D

Diwa

I was reading Stroustrup and came across the fact that operator ( )
checks good( ).
So, instead of explicitly checking for good( ) or fail( ) I could do
the following:
//
 
D

Diwa

Diwa said:
Useful and easy to remember summary...thanks....

Instead of explicitly checking for good( ) or fail( ) I could do :
// --------------------------------------------------------------------
if (iss_2 >> cfgSvrName)
std::cout << "cfgSvrName - ******" << cfgSvrName << "******\n";
// --------------------------------------------------------------------

Thanks,
Diwakar
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top