Good day,
What is the safest way to get a non-end of file value?
char_traits<cha r_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<ch ar_type>::eof() ? char_traits<cha r_type>::int_ty pe()?
char_traits<cha r_type>::eof() + 1?
while(!char_tra its<char_type>: :not_eof(char_t raits<char_type >::int_type(ran d())))?
mark 12 2451
"Mark A. Gibbs" <x_*********@ro gers.com_x> wrote in message
news:vR******** **@news04.bloor .is.net.cable.r ogers.com... Good day,
What is the safest way to get a non-end of file value? char_traits<cha r_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<ch ar_type>::eof() ? char_traits<cha r_type>::int_ty pe()? char_traits<cha r_type>::eof() + 1?
while(!char_tra its<char_type>: :not_eof(char_t raits<char_type >::int_type(ran d
())))? 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
"Howard" <al*****@hotmai l.com> wrote in message
news:Pc******** *************@b gtnsc04-news.ops.worldn et.att.net... "Mark A. Gibbs" <x_*********@ro gers.com_x> wrote in message news:vR******** **@news04.bloor .is.net.cable.r ogers.com... Good day,
What is the safest way to get a non-end of file value? char_traits<cha r_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<ch ar_type>::eof() ? char_traits<cha r_type>::int_ty pe()? char_traits<cha r_type>::eof() + 1?
while(!char_tra its<char_type>: :not_eof(char_t raits<char_type >::int_type(ran d ())))? 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<cha r_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
"P.J. Plauger" <pj*@dinkumware .com> wrote in message
news:8D******** *********@nwrdd c02.gnilink.net ... "Howard" <al*****@hotmai l.com> wrote in message news:Pc******** *************@b gtnsc04-news.ops.worldn et.att.net...
"Mark A. Gibbs" <x_*********@ro gers.com_x> wrote in message news:vR******** **@news04.bloor .is.net.cable.r ogers.com... Good day,
What is the safest way to get a non-end of file value? char_traits<cha r_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<ch ar_type>::eof() ? char_traits<cha r_type>::int_ty pe()? char_traits<cha r_type>::eof() + 1?
while(!char_tra its<char_type>: :not_eof(char_t raits<char_type >::int_type(ran d ())))? 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<cha r_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
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<cha r_type>::int_ty pe that is guaranteed to satisfy
char_traits<cha r_type>::not_eo f()?
(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_ty pe 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_ty pe c = traits::eof())
{
// whatever
return (success) ? traits::not_eof (c) : traits::eof();
}
would that be guaranteed to work as expected?
mark
Mark A. Gibbs wrote: but is there no simple way to generate a value of type char_traits<cha r_type>::int_ty pe that is guaranteed to satisfy char_traits<cha r_type>::not_eo f()?
'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_ty pe 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_ty pe 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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting
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
"Howard" <al*****@hotmai l.com> wrote in message
news:_V******** *************@b gtnsc04-news.ops.worldn et.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<cha r_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
"Mark A. Gibbs" <x_*********@ro gers.com_x> wrote in message
news:X%******** ******@news04.b loor.is.net.cab le.rogers.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<cha r_type>::int_ty pe that is guaranteed to satisfy char_traits<cha r_type>::not_eo f()?
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_ty pe 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_ty pe 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
"Mark A. Gibbs" <x_*********@ro gers.com_x> wrote in message
news:40******** ******@rogers.c om_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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 design standpoint,
I
wanted the class general enough to handle longer/more complex keys, so I
thought I should store this key value in the record class itself, and
query
the record types by a "canRead(std::istream&)" function. This would
|
by: |
last post by:
Sory about stupid question.
What are differences between uflow and underflow functions ?
Pls explains.
Tks
|
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;
class linebuf : public streambuf
{
|
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 <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
|
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.
| |
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 also when size was 2000 but line was
1200 bytes long.
There is no such problem with .Net 2002
For .Net 2003 I used :
#include <string>
#include <fstream>
|
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 these methods EXTRACT the data at one
point or another. The other problem is there appears to be NO
WAY to get at the actual buffer pointer (char*) of the characters
in the stream. There is a way to get the streambuf object
associated with the...
|
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 Tubbs
|
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
KhcAnjl/xFAEJb056UdQaCZqPfnGbqA+
=0Y8B
-----END PGP SIGNATURE-----
-----BEGIN PGP SIGNATURE-----
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |