C++ Write a Boolean value-returning function definition called OpenFile with 2 p
ID: 3800736 • Letter: C
Question
C++
Write a Boolean value-returning function definition called OpenFile with 2 parameters. The first parameter, a string, contains the name of the file that is entered for opening. The name of the file entered must be made available to the caller of this function. The second parameter, an ifstream, represents the input stream to use The function prompts the user for the name of the file to open and opens the file The function returns a value of true if the file was successfully open and a value of false if the file was not successfully opened.Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
bool openFile(ifstream& a, string fin)
{
a.open(fin.c_str());
if(a.is_open()) {
return true;
}
else {
return false;
}
}
int main()
{
ifstream a;
bool result;
string fileName;
cout<<"Enter file name to check status :";
cin >> fileName;
result = openFile(a,fileName);
if(result) {
cout<<"File successfully opened"<<endl;
}
else {
cout<<"File open status Failed"<<endl;
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.