-1

//entrada is the fstream I opened

if (entrada.is_open())
{
    while (!entrada.eof())
    {
        char palabra[1024];
        entrada >> palabra;
        if(entrada.eof())break;
        cout << palabra << endl;

    }

entrada.close();
}

the program shows me the text and doesnt finish the loop of the while, doesnt reach the .eof

  • 1
    Related [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/q/5605125/583833) – Borgleader Apr 25 '18 at 17:02
  • Possible duplicate of [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – underscore_d Apr 25 '18 at 17:06

1 Answers1

1

Change the loop to:

char palabra[1024];
while (entrada >> palabra )
{
    cout << palabra << endl;
}

See Why is iostream::eof inside a loop condition considered wrong?

R Sahu
  • 196,807
  • 13
  • 136
  • 247
  • i have done it, it still print my text infinite times – Erick Murillo Apr 25 '18 at 17:10
  • Technically, OPs code is more like `while (true) {entrada >> palabra; if (entrada.eof ()) break;}`, since `entrada.eof ()` is always `false`, in the loop condition (`eofbit` is only set by read operations, and the value of which is, immediately checked. If it is `true` at such check - one breaks out of the loop, before re-evaluation of loop condition). Hence I don't agree with links to such question, since OP uses `std::fstream::eof` correctly. – Algirdas Preidžius Apr 25 '18 at 17:21
  • @AlgirdasPreidžius, I see your point. Given the two options, I would pick my suggestion any day. The link has additional information that are very educational, IMO. – R Sahu Apr 25 '18 at 17:33