Friday 24 February 2017

C++ read from file last record shows two times

guys. My program based on class and read strem from file I
use operator<< to read from file. I have a problem with display last record frmo file - last record display two times.


void Model::showDatabase() {
cout << "Show database" << endl;
ifstream file(db);
string line;
const int maxIgnore = 1;
const int delim = ' ';
string val1, val2;
const char separator = ' ';
const int nameWidth = 6;
const int numWidth = 8;
int i=1;
cout << "\t\t Id. \t\t Numer \t\t Nazwa plyty" << endl;
while(file)
{
file>>val1;
file>>val2;
cout << "\t\t "< i++;
}
}

and my overload method


ostream& operator <<(ostream& myIn, Record& record)
{
cout << "save to file";
myIn << record.idRecord;
myIn << " ";
myIn << record.cdName;
myIn << "\n";
// myIn << "==\n";
return myIn;
}

and the last record from file shows 2x

No comments:

Post a Comment

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...