~traits::eof()?

M

Mark A. Gibbs

Good day,

What is the safest way to get a non-end of file value?
char_traits<char_type>::eof() will get me eof, but how can I safely and
consistently generate a value that is not eof? should i use
~char_traits<char_type>::eof()? char_traits<char_type>::int_type()?
char_traits<char_type>::eof() + 1?
while(!char_traits<char_type>::not_eof(char_traits<char_type>::int_type(rand())))?

mark
 
H

Howard

Mark A. Gibbs said:
Good day,

What is the safest way to get a non-end of file value?
char_traits<char_type>::eof() will get me eof, but how can I safely and
consistently generate a value that is not eof? should i use
~char_traits<char_type>::eof()? char_traits<char_type>::int_type()?
char_traits<char_type>::eof() + 1?
while(!char_traits said:

I think you are confused about eof(). It is a function, not a value.

Calling eof() on a stream checks if the stream has reached the end by a
previous read from the stream. When reading from a stream, you read until
reading fails. Then you can check if the failure was due to reaching the
end (as expected) or due to some kind of unexpected failure (an error). You
don't check the input and test it for being an "eof value", nor a "non-eof
value", because there is no such value in the first place.

Also, in C++, the ~ symbol is not used as a "not" operator. That symbol is
used to denote a destructor. The ! symbol is logical not. But as I stated
before, it has nothing to do with eof(), which is a function, not a value.

-Howard
 
P

P.J. Plauger

I think you are confused about eof(). It is a function, not a value.

Calling eof() on a stream checks if the stream has reached the end by a
previous read from the stream. When reading from a stream, you read until
reading fails. Then you can check if the failure was due to reaching the
end (as expected) or due to some kind of unexpected failure (an error). You
don't check the input and test it for being an "eof value", nor a "non-eof
value", because there is no such value in the first place.

I think you are confused about char_traits<char_type>::eof(). It
doesn't check for end-of-file, as does the feof function declared
in <stdio.h>. Rather, it returns the end-of-file code suitable
for a stream of char_type elements. You *do* check the input you
obtain from a stream buffer and test it for being an "eof value".
Also, in C++, the ~ symbol is not used as a "not" operator. That symbol is
used to denote a destructor. The ! symbol is logical not. But as I stated
before, it has nothing to do with eof(), which is a function, not a value.

Also, in C++ the ~ symbol is *also* used as a "not" operator. That
symbol is used to denote a bitwise inversion. When applied to the
value returned by eof(), it very likely will change it to a value
that does not compare equal to eof().

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
H

Howard

P.J. Plauger said:
I think you are confused about char_traits<char_type>::eof(). It
doesn't check for end-of-file, as does the feof function declared
in <stdio.h>. Rather, it returns the end-of-file code suitable
for a stream of char_type elements. You *do* check the input you
obtain from a stream buffer and test it for being an "eof value".

Interesting. I take it then that there are streams that you read where
you do check the value returned instead of using std::eof()? What kind of
streams would those be? Certainly not an actual file, right? I have to
admit I've never used a stream other than for normal file reading, or a
string stream for simple character conversions.

value.

Also, in C++ the ~ symbol is *also* used as a "not" operator. That
symbol is used to denote a bitwise inversion. When applied to the
value returned by eof(), it very likely will change it to a value
that does not compare equal to eof().

True. But in that case, any non-identity operation at all would suffice, if
all you wanted was a value different from whatever was returned. And as you
pointed out to the OP, the correct symbol is !, if you want to know when
something is "not true", in this case when a value is "not" the eof() value.

-Howard
 
M

Mark A. Gibbs

P.J. Plauger said:
Also, in C++ the ~ symbol is *also* used as a "not" operator. That
symbol is used to denote a bitwise inversion. When applied to the
value returned by eof(), it very likely will change it to a value
that does not compare equal to eof().

that was pretty much my thought. same for "eof() + 1" or something of
the like. but is there no simple way to generate a value of type
char_traits<char_type>::int_type that is guaranteed to satisfy
char_traits<char_type>::not_eof()?

(and if there isn't, mr. plauger, maybe you could stick one in your
library. you know where to find me for the royalties ^_-)

incidently, where i noticed the issue is in basic_streambuf::eek:verflow()
(and the like). it would be nice to do:

int_type overflow(int_type c = traits::eof())
{
// whatever
return (success) ? c : traits::eof();
}

but c could be eof() and the call could be successful, in which case
this code would generate spurious errors. the only thing i could think
of was:

int_type overflow(int_type c = traits::eof())
{
// whatever
return (success) ? traits::not_eof(c) : traits::eof();
}

would that be guaranteed to work as expected?

mark
 
D

Dietmar Kuehl

Mark said:
but is there no simple way to generate a value of type
char_traits<char_type>::int_type that is guaranteed to satisfy
char_traits<char_type>::not_eof()?

'not_eof()' is probably actually the function you are looking for: it
return a value different to 'eof()' even if you pass it 'eof()'. That,
you apparently want

traits::not_eof(traits::eof());
(and if there isn't, mr. plauger, maybe you could stick one in your
library. you know where to find me for the royalties ^_-)

It is already there but you are, of course, free to sent royalties
wherever you want to...
int_type overflow(int_type c = traits::eof())
{
// whatever
return (success) ? c : traits::eof();
}

but c could be eof() and the call could be successful, in which case
this code would generate spurious errors.

That is what 'not_eof()' is for:

return success? traits::not_eof(c): traits::eof();

The 'not_eof()' function has the property to return 'c' unless 'c' is
actually 'eof()' in which case 'not_eof()' returns some value != 'eof()'.
the only thing i could think
of was:

int_type overflow(int_type c = traits::eof())
{
// whatever
return (success) ? traits::not_eof(c) : traits::eof();
}

would that be guaranteed to work as expected?

Well, yes, that is how it is designed...
 
M

Mark A. Gibbs

Dietmar said:
'not_eof()' is probably actually the function you are looking for: it
return a value different to 'eof()' even if you pass it 'eof()'. That,
you apparently want

traits::not_eof(traits::eof());

ah, so traits::eof() can never be 0. perfect. thank you.
It is already there but you are, of course, free to sent royalties
wherever you want to...

*sigh*

mark
 
P

P.J. Plauger

Interesting. I take it then that there are streams that you read where
you do check the value returned instead of using std::eof()?

Please pay attention. THERE IS NO std::eof()!
What kind of
streams would those be? Certainly not an actual file, right? I have to
admit I've never used a stream other than for normal file reading, or a
string stream for simple character conversions.

Evidently. The function eof() IN TEMPLATE CLASS char_traits is used
by the iostreams code to detect when a stream buffer has encountered
end of stream. That certsainly includes actual files. You've probably
never had to use this member function eof() because it's part of the
under-the-hood implementation of iostreams.
True. But in that case, any non-identity operation at all would suffice, if
all you wanted was a value different from whatever was returned.

Yes. I was merely correcting your misinformation about ~.
And as you
pointed out to the OP, the correct symbol is !, if you want to know when
something is "not true", in this case when a value is "not" the eof()
value.

You're confusing me with someone else.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
P

P.J. Plauger

that was pretty much my thought. same for "eof() + 1" or something of
the like. but is there no simple way to generate a value of type
char_traits<char_type>::int_type that is guaranteed to satisfy
char_traits<char_type>::not_eof()?

Only if you have a candidate "character" in hand that you assuredly
want *not* turned into an eof. That's what not_eof is for.
(and if there isn't, mr. plauger, maybe you could stick one in your
library. you know where to find me for the royalties ^_-)

That's a helluva way to earn royalties, adding a nonstandard patch
to a kludge to an inelegant design.
incidently, where i noticed the issue is in basic_streambuf::eek:verflow()
(and the like). it would be nice to do:

int_type overflow(int_type c = traits::eof())
{
// whatever
return (success) ? c : traits::eof();
}

but c could be eof() and the call could be successful, in which case
this code would generate spurious errors. the only thing i could think
of was:

int_type overflow(int_type c = traits::eof())
{
// whatever
return (success) ? traits::not_eof(c) : traits::eof();
}

would that be guaranteed to work as expected?

Once you permit a valid character code to pun with eof(), it's hard
to say what "work" means anymore.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
M

Mark A. Gibbs

P.J. Plauger said:
How did you deduce that? It's almost certainly true, but I doubt
that it's guaranteed.

by assuming that i could use the result of not_eof() in a boolean
expression, which i now realize was not correct. the name threw me off
:/ sorry.

if all not_eof() is good for is guaranteeing a non-eof value, why does
it need the parameter? is it that worth the little convenience of
getting back a known result *most* of the time?

mark
 
M

Mark A. Gibbs

P.J. Plauger said:
Only if you have a candidate "character" in hand that you assuredly
want *not* turned into an eof. That's what not_eof is for.

am i the only one that finds that odd? if you have a candidate
"character" (int_type) in hand, and you want to be assured that you do
not have eof, wouldn't it have been more straightforward to have:

int_type d = eq_int_type(c, eof()) ? not_eof_character() : c;

at least then d is well defined.
That's a helluva way to earn royalties, adding a nonstandard patch
to a kludge to an inelegant design.

hey, i wouldn't be the first!
Once you permit a valid character code to pun with eof(), it's hard
to say what "work" means anymore.

sorry, what was that? just in case i wasn't clear, this overflow() is
basic_streambuf::eek:verflow(). basic_streambuf::eek:verflow() is supposed to
return eof() on failure, anything else otherwise (exactly what is
unspecified). the input character may be eof().

what way is there to possibly satisfy these conditions besides the line
above (or does that not work either)?

mark
 
P

P.J. Plauger

am i the only one that finds that odd? if you have a candidate
"character" (int_type) in hand, and you want to be assured that you do
not have eof, wouldn't it have been more straightforward to have:

int_type d = eq_int_type(c, eof()) ? not_eof_character() : c;

at least then d is well defined.

You're not the only one. See my comment immediately below.
hey, i wouldn't be the first!


sorry, what was that? just in case i wasn't clear, this overflow() is
basic_streambuf::eek:verflow(). basic_streambuf::eek:verflow() is supposed to
return eof() on failure, anything else otherwise (exactly what is
unspecified). the input character may be eof().

what way is there to possibly satisfy these conditions besides the line
above (or does that not work either)?

IIRC, you were muddling in the possibility that eof() could return zero.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top