-1

This is my code for writing and reading a text file but the problem here is that this code generating the last word again as in it is repeating the last work of the text file which it shouldn't. Can anyone tell what mistake im making?

The output of this program is:

Ahsan's

first

file.

file.

ofstream myfile;
ifstream infile;
string lol; 


myfile.open ("ahsan.txt");
myfile << "Ahsan's first file.\n";
myfile.close();

infile.open("ahsan.txt");
while(!infile.eof())
{
   infile>>lol;
   cout<<lol<<endl;


}
infile.close();
Ahsan Asif
  • 31
  • 7

1 Answers1

0

Try this instead of using .eof():

while(infile >> lol)
{
    cout<<lol<<endl;
}
yati sagade
  • 1,295
  • 9
  • 21