473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

~traits::eof()?

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

Jul 22 '05 #1
12 2396

"Mark A. Gibbs" <x_*********@rogers.com_x> wrote in message
news:vR**********@news04.bloor.is.net.cable.rogers .com...
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


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


Jul 22 '05 #2
"Howard" <al*****@hotmail.com> wrote in message
news:Pc*********************@bgtnsc04-news.ops.worldnet.att.net...
"Mark A. Gibbs" <x_*********@rogers.com_x> wrote in message
news:vR**********@news04.bloor.is.net.cable.rogers .com...
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

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
Jul 22 '05 #3

"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:8D*****************@nwrddc02.gnilink.net...
"Howard" <al*****@hotmail.com> wrote in message
news:Pc*********************@bgtnsc04-news.ops.worldnet.att.net...
"Mark A. Gibbs" <x_*********@rogers.com_x> wrote in message
news:vR**********@news04.bloor.is.net.cable.rogers .com...
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


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


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.

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().


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
Jul 22 '05 #4

P.J. Plauger wrote:
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::overflow()
(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
Jul 22 '05 #5
Mark A. Gibbs wrote:
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...
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #6

Dietmar Kuehl wrote:
'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.
(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...


*sigh*

mark

Jul 22 '05 #7
"Howard" <al*****@hotmail.com> wrote in message
news:_V*********************@bgtnsc04-news.ops.worldnet.att.net...
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".


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.
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().


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
Jul 22 '05 #8
"Mark A. Gibbs" <x_*********@rogers.com_x> wrote in message
news:X%**************@news04.bloor.is.net.cable.ro gers.com...
P.J. Plauger wrote:
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()?


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::overflow()
(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
Jul 22 '05 #9
"Mark A. Gibbs" <x_*********@rogers.com_x> wrote in message
news:40**************@rogers.com_x...
Dietmar Kuehl wrote:
'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.


How did you deduce that? It's almost certainly true, but I doubt
that it's guaranteed.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #10

P.J. Plauger wrote:
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.


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


sorry, what was that? just in case i wasn't clear, this overflow() is
basic_streambuf::overflow(). basic_streambuf::overflow() 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

Jul 22 '05 #11

P.J. Plauger wrote:
ah, so traits::eof() can never be 0. perfect. thank you.

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

Jul 22 '05 #12
"Mark A. Gibbs" <x_*********@rogers.com_x> wrote in message
news:F2*********************@twister01.bloor.is.ne t.cable.rogers.com...
P.J. Plauger wrote:
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.


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


sorry, what was that? just in case i wasn't clear, this overflow() is
basic_streambuf::overflow(). basic_streambuf::overflow() 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
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Kevin Saff | last post by:
Why are these two similar functions provided? (unget & putback) I'm working with a file format where the type of record to read in next is stored in the first two bytes of the record. From a...
2
by: | last post by:
Sory about stupid question. What are differences between uflow and underflow functions ? Pls explains. Tks
3
by: Christopher Benson-Manica | last post by:
Unless there is something wrong with the following complete C++ file, I think my quest to stream-ize our code will be at an end :( #include <sstream> #include <iostream> using namespace std;...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
2
GCC
by: Bo Hunter | last post by:
GCC 3.2 Redhat 9 Why does GCC have a problem with this? if( !( s1 >> Num( base, value )).eof() || ... I have to create a named Num object and everthing is fine.
6
by: Dave | last post by:
In .Net 2003 if a line, read from a text file is larger than a size parameter, the ifstream getline(buff, sze) put the file pointer to the EOF, so next peek() returns EOF. I saw this problem...
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
15
by: waltbrad | last post by:
Hello. I'm studying the book "C++ Primer Plus" by Stephan Prata. In chapter 6 he gives an exercise that reads from a file. The list is thus: 4 Sam Stone 2000 Freida Flass 100500 Tammy...
2
by: Arcturus | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkh6waMACgkQq5h2IFR18WMFrwCgv/PNAC8FTZCErvc0KHnx0zpC...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.