Why do I get an extra iteration (extra line printed) when this code completes? Does there need to be an e开发者_运维问答xtra newline at the EOF? I would prefer not having to add extra/special characters to mark the EOF.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream infile("dictionary.txt"); // one word per line
string text;
while(infile){
infile >> text;
cout << text << endl;
}
infile.close();
return 0;
}
try
while(infile>>text) cout << text << endl;
instead.
An input stream doesn't detect end-of-file until after you try to read past it. When you read the last word in the file, the input stream is still valid; on the next loop, infile >> text tries to read past EOF and fails, but the next line is still executed anyway.
The loop should look like this:
while (infile >> text)
cout << text << endl;
This way EOF will be detected before it tries to write to the output.
With your while-condition you check whether the stream is in a good state. Then you read from the stream, which may or may not succeed. Then you output the value of text. What you should do is:
while(infile >> text){
cout << text << endl;
}
At the end of the file, infile
might still evaluate to true
, but the following extraction of a word with infile >> text
fails. Even if it fails you still print out a line. A better way to do this would be to let the while loop check for successful extraction:
string text;
ifstream infile("dictionary.txt"); // one word per line
while (infile >> text) {
cout << text << endl;
}
infile.close();
You've already gotten a number of corrections, but perhaps a slightly different one is worth considering:
#include <iterator>
#include <iostream>
#include <string>
#include <fstream>
using namespace std; // not really a good idea, but harmless enough for now.
int main() {
ifstream infile("dictionary.txt");
copy(istream_iterator<string>(infile), istream_iterator<string>(),
ostream_iterator<string>(cout, "\n"));
return 0;
}
精彩评论