#include
<iostream>
#include
<fstream>
#include
<cstdlib>
#include
<string>
using namespace std;
void readFile(const
string& filename);
int main()
{
readFile("colors1.txt");
readFile("colors2.txt");
return 0;
}
void readFile(const
string& filename)
{
ifstream fin(filename.c_str());
string buffer;
if (!fin)
{
cerr << "Can't open " << filename
<< endl;
exit(1);
}
while (!fin.eof())
{
fin >> buffer;
cout << buffer << endl;
}
cout << "End of file" << endl;
fin.close();
}
***** OUTPUT *****
red
white
blue
blue
End of file
red
white
blue
End of file
Input Files
colors1.txt
colors2.txt
Questions
Are all the header files required? Why?
Is the fin.close() necessary at the end of the readFile() function?
Why is the filename passed as const string&?
How can you change the code to process either file correctly?