I need help editing this program using the eof() #include<iostream> #include<fst
ID: 3658661 • Letter: I
Question
I need help editing this program using theeof()
#include<iostream>
#include<fstream>// for files
using namespace std;
int main()
{
ofstream file_out("test.txt", ios::app);//creates the file
for(int i=1; i<=20; i++)//displays the integers code
{
file_out << i << endl;//closes the file
}
file_out.close();
ifstream file_in("test.txt");
int i;
while(file_in >> i)
{
cout << i << " ";//displays file onto the command prompt
}
file_in.close();//closes file
return 0;//ends program
}
Explanation / Answer
its simple when you are using ifstream to read the data instead of while(file_in>>i) you can use while(!file_in.eof()) this means that till the eof is not reached it will continue the loop... and then inside the while loop read the file using file_in >> i .. done . ... cheers!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.