Interpret istringstream::tellg as character position?

Discussion in 'C++' started by Fred Ma, May 24, 2004.

  1. Fred Ma

    Fred Ma Guest

    I'm using the stringstreams to get the numerical values of string
    tokens (the strings result from tokenizing a line of input elsewhere):

    #include <iostream>
    #include <sstream>
    using namespace std;

    int main(void)
    {
    istringstream iss("1.23DOG");
    float flt;

    cout << "isspos0=" << iss.tellg() << "\n" ;
    iss >> flt;
    cout << "flt=" << flt << "\n";
    cout << "isspos0=" << iss.tellg() << "\n" ;
    return 0;
    };

    This generates:

    isspos0=0
    flt=1.23
    isspos0=4

    I'm reading lots of warnings against interpreting tellg() as a an
    integral type, or as a byte offset from the beginning of the stream.
    This seems to be in the context of multibyte file streams, or file
    streams in microsoft text files, where two bytes are used to signify
    the end of line.

    For my purposes, I just want to use tellg() to determine if there are
    any residual characters left in the token, in which case I consider
    the token to be illegal. Is this "evil"? I don't anticipate dealing
    with multibyte data.

    Thanks.

    Fred
    --
    Fred Ma,
    Dept. of Electronics, Carleton University, Ottawa, Ontario, Canada
     
    Fred Ma, May 24, 2004
    #1
    1. Advertisements

  2. "Fred Ma" <> wrote in message
    news:...
    > I'm using the stringstreams to get the numerical values of string
    > tokens (the strings result from tokenizing a line of input elsewhere):
    >
    > #include <iostream>
    > #include <sstream>
    > using namespace std;
    >
    > int main(void)
    > {
    > istringstream iss("1.23DOG");
    > float flt;
    >
    > cout << "isspos0=" << iss.tellg() << "\n" ;
    > iss >> flt;
    > cout << "flt=" << flt << "\n";
    > cout << "isspos0=" << iss.tellg() << "\n" ;
    > return 0;
    > };
    >
    > This generates:
    >
    > isspos0=0
    > flt=1.23
    > isspos0=4
    >
    > I'm reading lots of warnings against interpreting tellg() as a an
    > integral type, or as a byte offset from the beginning of the stream.
    > This seems to be in the context of multibyte file streams, or file
    > streams in microsoft text files, where two bytes are used to signify
    > the end of line.
    >
    > For my purposes, I just want to use tellg() to determine if there are
    > any residual characters left in the token, in which case I consider
    > the token to be illegal. Is this "evil"? I don't anticipate dealing
    > with multibyte data.


    I guess not but why not try to read one more character? If that succeeds you
    know you have an invalid token. Seems a little less opaque as well.

    john
     
    John Harrison, May 24, 2004
    #2
    1. Advertisements

  3. Fred Ma

    Fred Ma Guest

    John Harrison wrote:
    >
    > "Fred Ma" <> wrote:
    > > I'm using the stringstreams to get the numerical values of string
    > > tokens (the strings result from tokenizing a line of input elsewhere)


    <...snip...>

    > > I'm reading lots of warnings against interpreting tellg() as a an
    > > integral type, or as a byte offset from the beginning of the stream.
    > > This seems to be in the context of multibyte file streams, or file
    > > streams in microsoft text files, where two bytes are used to signify
    > > the end of line.
    > >
    > > For my purposes, I just want to use tellg() to determine if there are
    > > any residual characters left in the token, in which case I consider
    > > the token to be illegal. Is this "evil"? I don't anticipate dealing
    > > with multibyte data.

    >
    > I guess not but why not try to read one more character? If that succeeds you
    > know you have an invalid token. Seems a little less opaque as well.



    Uhm....I didn't think of it. Good idea. Thanks.

    Fred
    --
    Fred Ma,
    Dept. of Electronics, Carleton University, Ottawa, Ontario, Canada
     
    Fred Ma, May 24, 2004
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aliki
    Replies:
    3
    Views:
    902
    LostAtC
    Sep 24, 2004
  2. Eivind Grimsby Haarr

    The tellg bug

    Eivind Grimsby Haarr, Sep 2, 2004, in forum: C++
    Replies:
    6
    Views:
    1,153
    Owen Jacobson
    Sep 4, 2004
  3. Replies:
    2
    Views:
    1,113
    Moonlit
    Sep 6, 2005
  4. peek() and tellg()

    , Sep 28, 2005, in forum: C++
    Replies:
    9
    Views:
    766
    P.J. Plauger
    Oct 2, 2005
  5. Jacek Dziedzic
    Replies:
    1
    Views:
    1,057
    Jacek Dziedzic
    Feb 6, 2007
  6. toton

    complexity for tellg()

    toton, Feb 20, 2007, in forum: C++
    Replies:
    12
    Views:
    1,460
    Pete Becker
    Feb 20, 2007
  7. IlyaK
    Replies:
    2
    Views:
    1,584
    IlyaK
    Jan 21, 2010
  8. mathieu
    Replies:
    4
    Views:
    769
    James Kanze
    Jul 28, 2013
Loading...