0

I've been learning about input from files, and I found this code snippet on stack exchange

int main(int argc, char * argv[])
{
std::fstream myfile("D:\\data.txt", std::ios_base::in);

float a;
while (myfile >> a)
{
    printf("%f ", a);
}

getchar();

return 0;
}

Other places i've looked said to use a char counter, and when char == eof thats when the loop stops.

What is while (myfile >> a) doing?

Podo
  • 709
  • 9
  • 30
  • http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – M.M Feb 13 '17 at 02:49
  • See [this](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – NathanOliver Feb 13 '17 at 02:49
  • Answers the question wonderfully. How did you both find that so quickly? Had you seen it before and just knew to reference it? – Podo Feb 13 '17 at 02:53
  • 1
    It comes up often on here, people post code that loops with the eof test and wonder why it breaks at the end – M.M Feb 13 '17 at 02:57
  • Have linked the questions since you indicated that this answers your question – M.M Feb 13 '17 at 02:57

0 Answers0