Hello everyone, I have been looking around here for awhile and on the internet but cant seem to find the answer to my question. I am making a program that will find the longest increasing subsequence from test bench files that are already created. The problem Im having right now is that I cant figure out how I can let the user type the file name of the bench file he wants to test and have it be used. Kinda confusing to explain, so here is the code I have so far.
int main()
{
string fileBench;
string line;
cout<<"Please enter the number of the bench file you wish to use: "<<endl;
cin>>fileBench;
ifstream bench (fileBench);//this doesnt work but this is what im trying to do basically
if (bench.is_open())
{
while(! bench.eof())
{
getline(bench,line);
cout<<line<<endl;
}
bench.close();
}
else cout<<"Unable to open file"<<endl;
system("Pause");
}
line 7: getline( cin, fileBench );
Some people like spaces in their filenames...
line 9: ifstream bench( fileBench.c_str() );
The iostream constructors don't take std::strings. Yes, obnoxious, I know...
OK, three things:
line 12: while (bench) or while (bench.good())
It is possible that something else goes wrong with the file, in which case the EOF flag can never be set, and you'd have an infinite loop. By testing the file for good()ness instead of just one termination condition, you guard against all termination conditions.