Reading from File - Help

I'm making a program that processes values from a .txt file and then calculates the standard deviation. However, I am stuck in the beginning of the program because, what I assume is happening, the values from the input file are not being read. What I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void readArrayFromFile(istream& ins, ostream& out, double a[], int& nvalues, int maxsize);
//pre:  ins is open and maxsize is the declared size of the array
//post: The values from ins have been read into a until the input
//      is exhausted or the array filled.  nvalues indicates the 
//      number of values read

int main()
{
	double values[100];
	int nvalues;
	string filename;

	ifstream ins;
	ofstream out;
	out.open("results.txt");
	out << "Assignment 10 for Matt Smith\n\n";

	out.setf(ios::fixed);
	out.setf(ios::showpoint);
	out.precision(2);

	//Input the file name and open it
	cout << "Please enter the name of the file to be read: ";
	cin >> filename;
	ins.open(filename.c_str());
	if (ins.fail())
	{
		cout << "Error opening input file.\n";
		exit(1);
	}
	
	//Input the values
	readArrayFromFile(ins, out, values, nvalues, 100);
	
	
	ins.close();
	out.close();
	return 0;
}

void readArrayFromFile(istream& ins, ostream& out, double a[], int& nvalues, int maxsize)
{
	int i = 0;
	while (i < maxsize && ins >> a[i])
	{
		i++;
	}
	nvalues = i;
	out << nvalues << " values were processed.\n";
}


When you enter "values.txt" for the filename, it always returns "Error opening the input file." and I cannot seem to get it to work. Even if I were to take out the user-inputed filename and simply say "ins.open("values.txt")," it returns that 0 values were processed in the output file.

Any help is greatly appreciated. Thanks!
Topic archived. No new replies allowed.