istringstream behaviour when no whitespace present in input



 DEVELOP > c-Plus-Plus > istringstream behaviour when no whitespace present in input

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Diwa"
Date: 03 Jan 2007 05:15:09 PM
Object: istringstream behaviour when no whitespace present in input
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
.

User: "Howard Hinnant"

Title: Re: istringstream behaviour when no whitespace present in input 03 Jan 2007 06:13:21 PM
In article <1167866108.732214.156370@v33g2000cwv.googlegroups.com>,
"Diwa" <shettydiwakar@gmail.com> wrote:

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
.
User: "Diwa"

Title: Re: istringstream behaviour when no whitespace present in input 04 Jan 2007 11:38:21 AM
Howard Hinnant wrote:

In article <1167866108.732214.156370@v33g2000cwv.googlegroups.com>,
"Diwa" <shettydiwakar@gmail.com> wrote:

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.

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
.
User: "Howard Hinnant"

Title: Re: istringstream behaviour when no whitespace present in input 04 Jan 2007 12:03:22 PM
In article <1167932301.434930.318480@v33g2000cwv.googlegroups.com>,
"Diwa" <shettydiwakar@gmail.com> wrote:

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
.
User: "Diwa"

Title: Re: istringstream behaviour when no whitespace present in input 04 Jan 2007 12:36:28 PM
Howard Hinnant wrote:

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....
.
User: "Diwa"

Title: Re: istringstream behaviour when no whitespace present in input 12 Jan 2007 04:03:22 PM
Diwa wrote:

Howard Hinnant wrote:

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....

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

User: "Diwa"

Title: Re: istringstream behaviour when no whitespace present in input 12 Jan 2007 04:02:25 PM
Diwa wrote:

Howard Hinnant wrote:

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.

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:
//
------------------------------------------------------------------------
if (iss_2 >> cfgSvrName)
std::cout << "cfgSvrName - ******" << cfgSvrName << "******\n";
//
------------------------------------------------------------------------
Thanks,
Diwakar
.






  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER