Here is the code :
When I run the previous code I get an "File myfile.txt could not be opened"Code:#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main (int argc, char * const argv[]) {
// insert code here...
int c; //input character
int i; //loop counter
string filename;
string input_line;
cout << "Enter a file name and press ENTER: ";
getline(cin, filename);
ifstream file_in(filename.c_str());
if (!file_in)
{
cout << "File " << filename;
cout << " could not be opened.";
return -1;
}
while (1)
{
for (i = 1; i <= 24 && !file_in.eof(); i++) {
getline(file_in, input_line);
cout << input_line << endl;
}
if (file_in.eof())
break;
cout << "More? (Press 'Q' and ENTER to quit.)";
getline(cin, input_line);
c = input_line[0];
if (c == 'Q' || c == 'q')
break;
}//end while
return 0;
}
Why does the program executes this !file_in.
I believe that file_in is not null.
thank you.