473,587 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

istream_iterato r question

The member function basic_ios::oper ator!() returns the bool result of
the basic_ios::fail () function which is true if either failbit or
badbit is set (This is per p. 616 of TC++PL by B. Stroustrup and p. 34
of Standard C++ IOStreams and Locales by Angelika Langer and Klaus
Kreft).

The implementation of the input stream iterator in the Langer and
Kreft book (see p. 129 to 130) uses the function:

void readElem()
{
if (istp!=0) if(!(*istp>>val ue)) istp=0;
}

in determining whether or not two istream iterators are equal as in
the following code:

vector<T> v(0);
ifstream f("input.txt" );
copy(istream_it erator<T>(f),is tream_iterator< T>(),back_inser ter(v));

In this example, istream_iterato r<T>(f) is equal to
istream_iterato r<T>() if istp is equal to 0 for both iterators. istp
is set equal to 0 if the failbit or badbit is set--not if the eofbit
is set.

What happens when the end of file has been reached? Does the loop
continue until a failbit or badbit is set? Would the following code
(which reflects eofbit status) serve as an alternative?

void readElem()
{
istp!=0&&istp->good()?*istp>> value:istp=0;
}
Jul 19 '05 #1
4 4149

"Bill Rudolph" <wp*******@yaho o.com> wrote in message
news:e4******** *************** ***@posting.goo gle.com...
The member function basic_ios::oper ator!() returns the bool result of
the basic_ios::fail () function which is true if either failbit or
badbit is set (This is per p. 616 of TC++PL by B. Stroustrup and p. 34
of Standard C++ IOStreams and Locales by Angelika Langer and Klaus
Kreft).

The implementation of the input stream iterator in the Langer and
Kreft book (see p. 129 to 130) uses the function:

void readElem()
{
if (istp!=0) if(!(*istp>>val ue)) istp=0;
}

in determining whether or not two istream iterators are equal as in
the following code:

vector<T> v(0);
ifstream f("input.txt" );
copy(istream_it erator<T>(f),is tream_iterator< T>(),back_inser ter(v));

In this example, istream_iterato r<T>(f) is equal to
istream_iterato r<T>() if istp is equal to 0 for both iterators. istp
is set equal to 0 if the failbit or badbit is set--not if the eofbit
is set.

What happens when the end of file has been reached? Does the loop
continue until a failbit or badbit is set? Would the following code
(which reflects eofbit status) serve as an alternative?

void readElem()
{
istp!=0&&istp->good()?*istp>> value:istp=0;
}


No that would be wrong. fail refers to the failure to extract an element,
eof refers to a read past the end of file. It is possible for eof to be true
when fail is false. For instance suppose that last item in a file is an
integer with no following whitespace, then in order to read the integer the
program is going to have to read past the end of the file, setting eof to
true, but an element has still been extracted so fail is false and
processing should continue.

john
Jul 19 '05 #2
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<bh******* ****@ID-196037.news.uni-berlin.de>...

No that would be wrong. fail refers to the failure to extract an element,
eof refers to a read past the end of file. It is possible for eof to be true
when fail is false. For instance suppose that last item in a file is an
integer with no following whitespace, then in order to read the integer the
program is going to have to read past the end of the file, setting eof to
true, but an element has still been extracted so fail is false and
processing should continue.


I assume that you are referring to my alternative readElem().

Assume the Langer and Kreft implementation (p. 130) of
istream_iterato r::operator++(i nt)

istream_iterato r istream_iterato r::operator++(i nt)
{
istream_iterato r tmp=*this;
readElem();
return tmp;
}

and the Stroustrup implementation (p. 529) of std::copy

Out copy(In first,In last,Out res)
{
while (first!=last) *res++=*first++ ;
return res;
}

Suppose that the last item in a file is an integer with no following
whitespace. That is, the file ends with 98 12345<eof>.

In std::copy, first does not equal last and *first++ is called. A
temporary istream_iterato r named tmp is created in which istp points
to the same stream and value equals 98. istream_iterato r::readElem()
is called. istp does not equal 0 and therefore *istp>>value is called.
istream_iterato r::value is set to 12345. ios_base::eofbi t is set but
iosbase::failbi t is not set. The function basic_ios::fail () returns
false and !(*istp>>value) returns false. Therefore, istp is not set
equal to 0. The istream_iterato r returned from
istream_iterato r::operator++(i nt) is the temporary that was created.

In the next iteration, first does not equal last as istp does not
equal 0. *first++ is called. A temporary is created in which istp
points to the same stream and value equals 12345.
istream_iterato r::readElem() is called. istp does not equal 0 and
therefore *istp>>value is called. The extraction fails as there is
nothing to extract. The iosbase::failbi t is set, basic_ios::fail ()
returns true, and !(*istp>>value) returns true. istp is set equal to
0. The istream_iterato r returned from
istream_iterato r::operator++(i nt) is the temporary that was created
that contains value equal to 12345.

In the next iteration, first equals last as istp equals 0 and res is
returned from the copy function.

Is this the correct interpretation of what happens when the end of the
file is reached? If so, I don't see anything wrong with the
alternative:

void readElem()
{
istp!=0&&istp->good()?*istp>> value:istp=0;
}
Jul 19 '05 #3

"Bill Rudolph" <wp*******@yaho o.com> wrote in message
news:e4******** *************** **@posting.goog le.com...
"John Harrison" <jo************ *@hotmail.com> wrote in message

news:<bh******* ****@ID-196037.news.uni-berlin.de>...

No that would be wrong. fail refers to the failure to extract an element, eof refers to a read past the end of file. It is possible for eof to be true when fail is false. For instance suppose that last item in a file is an
integer with no following whitespace, then in order to read the integer the program is going to have to read past the end of the file, setting eof to true, but an element has still been extracted so fail is false and
processing should continue.


I assume that you are referring to my alternative readElem().

Assume the Langer and Kreft implementation (p. 130) of
istream_iterato r::operator++(i nt)

istream_iterato r istream_iterato r::operator++(i nt)
{
istream_iterato r tmp=*this;
readElem();
return tmp;
}

and the Stroustrup implementation (p. 529) of std::copy

Out copy(In first,In last,Out res)
{
while (first!=last) *res++=*first++ ;
return res;
}

Suppose that the last item in a file is an integer with no following
whitespace. That is, the file ends with 98 12345<eof>.

In std::copy, first does not equal last and *first++ is called. A
temporary istream_iterato r named tmp is created in which istp points
to the same stream and value equals 98. istream_iterato r::readElem()
is called. istp does not equal 0 and therefore *istp>>value is called.
istream_iterato r::value is set to 12345. ios_base::eofbi t is set but
iosbase::failbi t is not set. The function basic_ios::fail () returns
false and !(*istp>>value) returns false. Therefore, istp is not set
equal to 0. The istream_iterato r returned from
istream_iterato r::operator++(i nt) is the temporary that was created.

In the next iteration, first does not equal last as istp does not
equal 0. *first++ is called. A temporary is created in which istp
points to the same stream and value equals 12345.
istream_iterato r::readElem() is called. istp does not equal 0 and
therefore *istp>>value is called. The extraction fails as there is
nothing to extract. The iosbase::failbi t is set, basic_ios::fail ()
returns true, and !(*istp>>value) returns true. istp is set equal to
0. The istream_iterato r returned from
istream_iterato r::operator++(i nt) is the temporary that was created
that contains value equal to 12345.

In the next iteration, first equals last as istp equals 0 and res is
returned from the copy function.

Is this the correct interpretation of what happens when the end of the
file is reached? If so, I don't see anything wrong with the
alternative:

void readElem()
{
istp!=0&&istp->good()?*istp>> value:istp=0;
}


Yes I think your interpretation is correct, but I still think your version
of readElem will cause the copy loop to terminate wrongly in some
situations. It just feels wrong to be using the eof bit in this situation,
whether a value has bee extracted it the important issue.

But I could easily be wrong, why not code it up and find out?

john
Jul 19 '05 #4
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<bh******* ****@ID-196037.news.uni-berlin.de>...
Yes I think your interpretation is correct, but I still think your version
of readElem will cause the copy loop to terminate wrongly in some
situations.


Assume the readElem() from the Langer and Kreft implementation. That
is,

void readElem()
{
if (istp!=0) if(!(*istp>>val ue)) istp=0;
}

An example of extracting an integer was illustrated. Suppose though,
that something else was extracted, say a BookCharacter using:

istream&
operator>>(istr eam& is,BookCharacte r& t)
{
return !is.good()?is:t .input(is);
//or return is.good()?t.inp ut(is):is;
}

Also, assume that the last item of the stream has been extracted. As
before, istp does not equal 0. ios_base::eofbi t is set but
iosbase::failbi t is not set. The istream_iterato r returned from
istream_iterato r::operator++(i nt) is the temporary that was created.

In the next iteration, first does not equal last as istp does not
equal 0. *first++ is called. A temporary is created in which istp
points to the same stream and value is a copy of the object which
contains the last item extracted from the stream.
istream_iterato r::readElem() is called. istp does not equal 0 and
therefore *istp>>value --i.e., the function istream&
operator>>(istr eam&,BookCharac ter&)--is called. !is.good() is true as
ios_base::eofbi t has been set. Therefore istream& is returned.
(!(*istp>>value )) is false as neither ios_base::failb it nor
ios_base::badbi t has been set. istp is not set to 0. The
istream_iterato r returned from istream_iterato r::operator++(i nt) is
the temporary that was created.

With this extractor, there is a loop that continues until there is a
crash due to insufficient memory. istp is 0. value is the object that
contains the last item extracted.

Is this interpretation correct? My version of readElem would not
cause this copy loop to terminate wrongly in this circumstance.
Jul 19 '05 #5

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

Similar topics

3
2146
by: NPC | last post by:
Hi, Is there any way to use an istream_iterator<> in a way which inserts each element at the end of a newline character rather than a space character? Could be it looks for any type of whitespace - not sure about that. In particular, it would be nice to use it in a way which is similar to an ostream_iterator: std::vector<std::string> myVec; /* add stuff to myVec */
9
3173
by: Alex Vinokur | last post by:
------ foo.cpp : BEGIN ------ #include <cassert> #include <vector> #include <string> #include <iostream> #include <iterator> #include <fstream> using namespace std;
0
4979
by: Richo11 | last post by:
This is probably a silly question but I am learning sorry... I am trying to read in a series of strings from standard input using the istream_iterator member function, how does istream_iterator<string>eos work. How does it know that you have finished entering data? :confused: any help appreciated. Cheers Richo
2
1643
by: alberto | last post by:
I am learning STL with the book STL Tutorial and Reference guide (1 edition), the following example don't run : int main() { // Initialize array a with 10 integers: int a = {12, 3, 25, 7, 11, 213, 7, 123, 29, -31}; // Find the first element equal to 7 in the array: int* ptr = find(&a, &a, 7);
6
8472
by: George | last post by:
Hi All, I'm trying to learn c++/stl. I'd like a fancy way to read lines of an ascii file into vector of stringbufs. I made a first attempt, but the compiler complains about private constructors in streambuf. I'd like to use algorithms instead of a loop, but I don't know if that is possible. Any ideas? Thanks in advance.
3
3808
by: jmoy.matecon | last post by:
I get an error while compiling the following program: int main() { vector<int> v(istream_iterator<int>(cin), istream_iterator<int>()); copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\n")); } The errors I get are is:
5
2820
by: Pradeep | last post by:
Hi All, I am facing some problem using istream_iterator for reading the contents of a file and copying it in a vector of strings.However the same thing works for a vector of integers. The code that doesn't work is std::vector<std::stringvecStr; std::ifstream fstrRead("Test.txt");
2
1803
by: Juha Nieminen | last post by:
I'm using gcc 3.3.5. This code: std::set<std::stringt(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>()); gives a strange error message: error: cannot use `::' in parameter declaration If I try it this way:
0
1334
by: thomas | last post by:
hi guys, I got a question about istream_iterator. ---code--- istream_iterator<intinput(cin), end; vector<intvec; copy(input, end, inserter(vec, vec.begin())); ---code--- the problem is that I don't want to copy all the data from input to
0
7924
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, 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...
0
8219
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, 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...
0
8349
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 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...
1
7978
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,...
1
5722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
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
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
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...

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.