You need to decide whether you're going to pass a string, or a file name. If you pass a string, then the caller needs to pass the string, and the function needs to be written to expect a file name.
If you decide to pass a stream, the caller needs to open and pass the stream, and the function needs to be written expecting a stream that it'll just use.
option A:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
bool readFile(std::string const &fileName);
int main(void)
{
string fileName;
cout << "Enter the file name: ";
cin >> fileName;
readFile(fileName);
}
bool readFile(std::string const &fileName)
{
ifstream file(fileName);
return true;
}
Option B:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
bool readFile(std::istream& file);
int main(void)
{
string fileName;
cout << "Enter the file name: ";
cin >> fileName;
ifstream file(fileName);
readFile(file);
}
bool readFile(std::istream& fileName)
{
return true;
}
Either one can work -- you just need to be consistent between the caller and callee. By strong preference, you want to be as consistent as possible throughout a given code base as well.