0

My code is as follows: Predicted output is not equal to actual output!

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    ofstream fout("abc.txt");
    fout<<"Hello World\nHello World";
    fout.close();

    ifstream fin;
    fin.open("abc.txt");
    if(!fin)
    {
        cout<<"File not open";
        exit(0);
    }
    char ch;
    while(fin.eof()==0)
    {
        fin.get(ch);
        cout<<ch;
    }             
    fin.close();
                    
    return 0;
}

The predicted output according to me is

Hello World
Hello World

But the output shown on console is

Hello World
Hello Worldd

Why d is printed two times? Here, I tried to read content of the file character by character.

463035818_is_not_a_number
  • 64,173
  • 8
  • 58
  • 126

0 Answers0