By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Interpret istringstream::tellg as character position?

P: n/a
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, fm*@doe.carleton.ca
Dept. of Electronics, Carleton University, Ottawa, Ontario, Canada
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Fred Ma" <fm*@doe.carleton.ca> wrote in message
news:40***************@doe.carleton.ca...
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
Jul 22 '05 #2

P: n/a
John Harrison wrote:

"Fred Ma" <fm*@doe.carleton.ca> 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, fm*@doe.carleton.ca
Dept. of Electronics, Carleton University, Ottawa, Ontario, Canada
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.