Please send questions to
st10@humboldt.edu .
/*--------------------------------------------------
created by st10 at Mon Apr 23 10:34:32 PDT 2007
--------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
/*--------------------------------------------------
Contract: get_file_contents : double[] int char* -> bool
Purpose: try to open file <infile_name> for reading,
and try to read its first <desired_size>
values into array <contents>. Return true if succeeded,
and false if failed.
Examples: if const int NUM_SCORES = 4; and
double scores[NUM_SCORES]; and
file scores.txt contains:
90
80.5
85.5
99
then if the user runs
get_file_contents(scores, NUM_SCORES, "scores.txt")
then, the array scores will now have the contents
{90, 80.5, 85.5, 99}
--------------------------------------------------*/
bool get_file_contents(double contents[], int desired_size,
char* infile_name)
{
double next_value;
ifstream instream;
instream.open(infile_name);
if (instream.fail())
{
return false;
}
// can only reach here if COULD open file for reading...
for (int i = 0; i < desired_size; i++)
{
// see how DON'T need prompt, here? Just reading from
// input file stream;
instream >> next_value;
contents[i] = next_value;
}
instream.close();
return true;
}