ifstream duplicate char?
Posted: Sun Sep 12, 2010 5:12 pm
I have a program that reads in a file character by character, and for some reason the last character is always duplicated. I'm thinking that maybe I'm messing up my loop or something.
The file (testfile.txt):
The code reading the file:
the output always ends up being:
test test testt
The file (testfile.txt):
Code: Select all
test test test
Code: Select all
if(file.is_open()) {
string str;
while(!file.eof()) {
char c;
file.get(c);
cout << c;
if(c != ' ' && c != '\n' && c != '\r')
str += c;
else {
Token token(str);
tokens.push_back(token);
str = "";
}
}
if(str.size() > 0) {
Token token(str);
tokens.push_back(token);
}
}
test test testt