mardi 31 octobre 2017

Operator >> with ifstream

I apologize if this is a duplicate, but I can't seem to find a concrete answer to my question. Assume I have a text file below:

Product id  Cost  Tax type
10012       34.56
10023       45.67 H
10234       12.32 P
10056       67.54
10029       54.12 H
10034       96.30 

I'm running through a while loop to read each line and then save them into a variable and then create an instance of a object depending whether it is a taxable product or not.

int _Id;
double _Price;
char _Tax;
iProduct* temp;

is >> _Id >> _Price; //First question: How does it know to read until the end 10012 into _Id and 34.56 into _Price?
if (is.get() != '\n') 
{
    is.get(_Tax); //Second question is at the bottom
    temp = new TaxableProduct(_Id, _Price, _Tax);
}
else
{
    temp = new Product(_Id, _Price);
}

My first question is how or when does the operator >> know what to read until? Second question is how does the is.get(_Tax) know that the last character is the _Tax?

Aucun commentaire:

Enregistrer un commentaire