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

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

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

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. Eivind Grimsby Haarr

    The tellg bug

    Eivind Grimsby Haarr, Sep 2, 2004, in forum: C++
    Replies:
    6
    Views:
    930
    Owen Jacobson
    Sep 4, 2004
  2. John Harrison
    Replies:
    0
    Views:
    488
    John Harrison
    Oct 18, 2004
  3. Catalin Pitis
    Replies:
    0
    Views:
    532
    Catalin Pitis
    Oct 20, 2004
  4. Replies:
    2
    Views:
    920
    Moonlit
    Sep 6, 2005
  5. peek() and tellg()

    , Sep 28, 2005, in forum: C++
    Replies:
    9
    Views:
    586
    P.J. Plauger
    Oct 2, 2005
Loading...

Share This Page