If the user types \"employees.txt\" the program should output: \"Successfully op
ID: 3730381 • Letter: I
Question
If the user types "employees.txt" the program should output: "Successfully opened file".
If the user types anything else, such as "hello", the output should say "Unable to open the file for input".
The file should open or not open, accordingly. What needs to be changed for this to work?
[code]
#include
#include
#include
using namespace std;
int main()
{
string filename;
cout << "Enter the input file name: ";
cin >> filename;
ifstream ifs;
ifs.open("employees.txt");
if (!ifs.is_open())
{
cerr << "Unable to open the file for input ";
}
else
{
cout << "Successfully opened file ";
}
system("pause");
return 0;
}
[/code]
Explanation / Answer
Hi.. Please check above cpp code.
Main.cpp
/*#include
#include
*/
#include <iostream>;
#include <fstream>
using namespace std;
int main()
{
string filename;
cout << "Enter the input file name: ";
cin >> filename;
ifstream ifs;
ifs.open("employees.txt");
if (!ifs.is_open())
{
cerr << "Unable to open the file for input ";
}
else
{
cout << "Successfully opened file ";
}
return 0;
}
From the above code i have added include files for the code.
Please check it and let me know any issues. Thank you. All the best.