Hi,
I'm a student and fairly new to C++. I need help with this school assignment. It's supposed to demonstrate fstream by reading the contents of different files and displaying them on the monitor. It works perfectly the first time, but after that I get an error message. Please help.
Thanx,
John Touron
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
class FILEMANIP
{
char filePath[100];
ifstream input;
public:
void GetFilePath();
void OpenFile() { input.open(filePath); }
int CheckOpen();
void DisplayContents();
void CloseFile() { input.close(); }
;
} fileManip;
bool cont();
int main ()
{
do
{
fileManip.GetFilePath();
fileManip.OpenFile();
fileManip.CheckOpen();
fileManip.DisplayContents();
fileManip.CloseFile();
} while (cont());
return 0;
}
bool cont()
{
char x;
cout << "Do you wish to display the contents of another file (Y/N)? ";
cin >> x;
switch (toupper(x))
{
case 'Y':
cout << endl;
return true;
default:
cout << endl;
return false;
}
}
void FILEMANIP::GetFilePath()
{
int x = 0;
cout << "Enter path and file name of the file to be read from:\n\n";
cin.getline(filePath,100);
cout << endl;
}
int FILEMANIP::CheckOpen()
{
if (input.fail())
{
cerr << "*** ERROR: Cannot open " << filePath
<< " for input.\n";
return EXIT_FAILURE;
}
return 0;
}
void FILEMANIP::DisplayContents()
{
char nextChar;
input.get(nextChar);
while (!input.eof())
{
cout << nextChar;
input.get(nextChar);
}
cout << endl << endl;
}