Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > istringstream behaviour when no whitespace present in input

Reply
Thread Tools

istringstream behaviour when no whitespace present in input

 
 
Diwa
Guest
Posts: n/a
 
      01-03-2007
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

 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      01-04-2007
In article <(E-Mail Removed) .com>,
"Diwa" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Diwa
Guest
Posts: n/a
 
      01-04-2007
Howard Hinnant wrote:
> In article <(E-Mail Removed) .com>,
> "Diwa" <(E-Mail Removed)> 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

 
Reply With Quote
 
Howard Hinnant
Guest
Posts: n/a
 
      01-04-2007
In article <(E-Mail Removed) .com>,
"Diwa" <(E-Mail Removed)> 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
 
Reply With Quote
 
Diwa
Guest
Posts: n/a
 
      01-04-2007
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....

 
Reply With Quote
 
Diwa
Guest
Posts: n/a
 
      01-12-2007
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

 
Reply With Quote
 
Diwa
Guest
Posts: n/a
 
      01-12-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Splitting text at whitespace but keeping the whitespace in thereturned list MRAB Python 3 01-26-2010 11:36 PM
Structure using whitespace vs logical whitespace cmdrrickhunter@yaho.com Python 10 12-16-2008 03:51 PM
OS present but not present. XPD NZ Computing 4 04-11-2007 11:22 PM
Whitespace where I don't want whitespace! Oli Filth HTML 9 01-17-2005 08:47 PM
Schema: express that "@a present if and only if @b present", where @a, @b are attributes Ralf Wahner XML 5 12-24-2003 11:37 AM



Advertisments