Once this member function is called, the stream object can be used to open another file, and the file is available again to be opened by other processes.
I tried reusing the ifstream variable infile in the code below to open a second text file but the data I get is incorrect and is possibly garbage. For instance I get 899 and 131 instead of 2 and 5706.
Using a new ifstream variable for opening the second text file gets the correct data.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
int n, pof=0, pos=0, f, s, l=0, w ;
int i = 0;
ifstream infile("F:\\Basic-2-testdata\\2.in");
if(infile.is_open())
{
infile >> n;
while (infile.good())
{
infile>>f>>s;
pof += f;
pos += s;
//cout<<"Round"<<++i<<" "<<pof<<" "<<pos<<" Lead "<<pof-pos<<endl;
if (abs(pof-pos)>abs(l)){l = pof-pos;}
}
infile.close(); // close infile here
if (l>0) {w=1;} else {w=2;}
cout<<"Winner is player"<<w<<" with greatest lead of "<<abs(l)<< endl;
infile.open("F:\\Basic-2-testdata\\2.out"); // try to reuse infile here
if (infile.is_open())
{
infile>>f>>s;
cout<<"Test out data: Winner is player"<<f<<" Lead "<<s<<endl;
infile.close();
}
else
{
cout << "unable to open outfile"<< endl;
}
}
else
{
cout << "unable to open infile" << endl;
}
system("pause");
return 0;
}