

Example why stream :: good is Wrong?
source link: https://www.codesd.com/item/example-why-stream-good-is-wrong.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Example why stream :: good is Wrong?
I gave an answer which I wanted to check the validity of stream each time through a loop here.
My original code used good
and looked similar to this:
ifstream foo("foo.txt");
while (foo.good()){
string bar;
getline(foo, bar);
cout << bar << endl;
}
I was immediately pointed here and told to never test good
. Clearly this is something I haven't understood but I want to be doing my file I/O correctly.
I tested my code out with several examples and couldn't make the good
-testing code fail.
First (this printed correctly, ending with a new line):
bleck 1
blee 1 2
blah
ends in new line
Second (this printed correctly, ending in with the last line):
bleck 1
blee 1 2
blah
this doesn't end in a new line
Third was an empty file (this printed correctly, a single newline.)
Fourth was a missing file (this correctly printed nothing.)
Can someone help me with an example that demonstrates why good
-testing shouldn't be done?
They were wrong. The mantra is 'never test .eof()
'.
- Why is iostream::eof inside a loop condition considered wrong?
Even that mantra is overboard, because both are useful to diagnose the state of the stream after an extraction failed.
So the mantra should be more like
Don't use
good()
oreof()
to detect eof before you try to read any further
Same for fail()
, and bad()
Of course stream.good
can be usefully employed before using a stream (e.g. in case the stream is a filestream which has not been successfully opened)
However, both are very very very often abused to detect the end of input, and that's not how it works.
A canonical example of why you shouldn't use this method:
std::istringstream stream("a");
char ch;
if (stream >> ch) {
std::cout << "At eof? " << std::boolalpha << stream.eof() << "\n";
std::cout << "good? " << std::boolalpha << stream.good() << "\n";
}
Prints
false
true
See it Live On Coliru
Recommend
-
226
This site can’t be reached The webpage at https://www.java67.com/2017/07/how-to-sort-map-by-values-in-java-8.html might be temporarily down or it may have moved permanently to a new...
-
12
Today we learn about Video Streaming with Node.js and HTML5. We will capture the thumbnail from the stored video and will use it as a poster to the HTML5 video tag. We will use the FFmpeg library to capture the thumbnail from the video. You c...
-
9
Why you're probably doing endpoint security all wrong The traditional combination of username and password to secure company devices and systems is outdated and could be putting your business at risk. Employing an AI based...
-
3
Why is Wrong Stacktrace Printed for My Code?2021-03-13Python531 words 3 mins read 23 times readThe other day, when I was updating the soruce code wit...
-
9
-
4
Why Our Predictions About AI's Abilities Are Always Wrong 4405 members Technology Technology on Digg: the best articles, vi...
-
7
(Neo4j Blog)←[:BACK] This Week in Neo4j: Batch vs. Stream, UX Survey, Kafka Example, Jupyter/Docker for Data Science, Deep Learning ...
-
11
A Simple Fun Example of How a Computer Reads Intent Wrong I read an article about India and wanted to know how much money “2 lakh” is in Euro. The search engine responded as follows: Search re...
-
8
Samsung Galaxy S21 FE Review: Good phone, wrong priceThe Galaxy S20 FE (Fan Edition) was one of the best smartphones of 2020 back when it was released. This w...
-
4
PPP 368 | Where Good Leaders Go Wrong, With Author Jim WetrichOct 28, 2022Leadership...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK