Thursday, 20 October 2016
c++ - Why am I reading the last word in my file twice?
Answer
I'm trying to read in the text from a file and this is what I am doing:
int main(int argc, char* argv[])
{
ifstream inFile;
inFile.open(argv[1]);
string item;
while(inFile.good())
{
inFile >> item;
cout << item << " " << endl;
}
For some reason it will read the last word in the file twice. I tried using a count variable to keep track of how many times it enters the while loop and it always enters one time more then the total number of line in the file. I think this is happening because the inFile.good() statement is not returning false soon enough. How can if fix it?
Subscribe to:
Post Comments (Atom)
c++ - Does curly brackets matter for empty constructor?
Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...
-
In K-PAX , there are two possible explanations: Prot is crazy. He is truly an alien. There are facts that indicate 1, but also 2. Examples: ...
-
I found this excellent tutorial on regular expressions and while I intuitively understand what "greedy", "reluctant" an...
-
Why this works? I mean, accessing the private variable. class Test { private $q = 0; public function __construct() { $this-...
No comments:
Post a Comment