Interpret istringstream::tellg as character position?

F

Fred Ma

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
 
J

John Harrison

Fred Ma said:
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
 
F

Fred Ma

John said:
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
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top