#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
/***********************************************************
* READ DATA: Read the data from a file
* INPUT: fileName. the file to be read
* OUTPUT: data. The data from the file
* IMPORTANT: You are not allowed to change readData() in any way
***********************************************************/
string readData(const string & fileName) throw (string, bool, int)
{
// missing file case
if (fileName.size() == 0)
throwtrue; // "ERROR: No filename specified\n"
// attempt to open the file
ifstream fin(fileName.c_str());
if (fin.fail())
throw fileName; // "ERROR: Invalid filename \"" << s << "\"\n"
// attempt to read the data
string data;
getline(fin, data);
bool moreData = !fin.eof();
fin.close();
// empty file
if (data.size() == 0)
throw 0; // "ERROR: The file was empty\n"
// message too long
if (data.size() > 140)
throw 1; // "ERROR: The message exceeded 140 characters\n"
// more than one line of data
if (moreData)
throw 2; // "ERROR: The message was longer than 1 line\n"
// success
return data;
}
/**********************************************************************
* MAIN: This function will prompt the user for the file and display the
* contents on the screen.
***********************************************************************/
int main()
{
// get the filename
string fileName;
cout << "What is the filename? ";
getline(cin, fileName);
// read the data
string data = readData(fileName);
try // is this right?
{
string readData(fileName);
}
catch (int integer)// also check to see if i did it right here correctly
{
switch (integer)
{
case 0:
cout << "ERROR: The file was empty\n";
break;
case 1:
cout << "ERROR: The message exceeded 140 characters\n";
break;
case 2:
cout << "ERROR: The message was longer than 1 line\n";
break;
}
}
// display the results:
cout << "The important fact: \""
<< data
<< "\"\n";
return 0;
}