Data file will not read, keep getting error.

Data file won't read, i have tried everything and have been on so many different forums. I made sure the file is the same name, i created the .txt file, copied its name and pasted it in, so it is accurate. Please help. If you find any other errors with it, please feel free to inform me.

the .txt file is just a list of grades ranging from 1-100 to represent test grades.

example:
97
37
81
75
...
ect


here is 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdafx.h>
#include<iostream>
#include<fstream>
#include<cmath>
#include<string>
using namespace std;

const int ArrayMax=1000;
double maxval(const double x[], int n);
double minval(const double x[], int n);
double aveval(const double x[], int n);
double standev(const double x[], int n);

int main ()
{
    int data=0;
    double y[ArrayMax], array;
    ifstream file;
    
    file.open("Grades_Spring2012.txt");
    
    if(file.fail())
    {
                   cerr<<"Error opening input file."<<endl;
                   system("pause");
                   exit(0);
    }
    
    file>>array;
    
    while ((data<ArrayMax) && !file.eof())
    {
          y[data]=array;
          file>>array;
    }
    
    cout<<"The Maximum score is "<<maxval(y, data)<<endl;
    cout<<"The Minimum score is "<<minval(y, data)<<endl;
    cout<<"The Average score is "<<aveval(y, data)<<endl;
    
    file.close();
    
    system("pause");
    return 0;
}

double maxval(const double x[], int n)
{
       double maxVal, k;
       maxVal=x[0];
       
       for (int k=1; k<n; ++k)
       {
           if (x[k]>maxVal)
           maxVal=x[k];
       }
       
       return maxVal;
}

double minval(const double x[], int n)
{
       double min;
       min=x[0];
       for (int k=1; k<=n-1; ++k)
       {
           if (x[k]<min)
           min=x[k];
       }
       return min;
}

double aveval(const double x[], int n)
{
       double sum(0);
       
       for (int k=0; k<n; ++k)
       {
           sum += x[k];
       }
       
       return sum/n;
}

double variance(const double x[], int n)
{
       double sum(0), mu;
       mu=aveval(x,n);
       
       for (int k=0; k<n; ++k)
       {
           sum += (x[k]-mu)*(x[k]-mu);
       }
       
       return sum/(n-1);
}

double standev(const double x[], int n)
{
    return sqrt(variance(x,n));
}

Last edited on
If you're running from inside an ide, make sure the file you're trying to read is in the working directory. If you're running from the command line, ensure the file you're trying to read is in the same directory. Consider using the full path to the file if you can't resolve your issue.
I made sure the file is in the project folder, if that is what you mean. I am using Dev-C++ and MVS11 if that helps.

How would i go about using full path?

Last edited on
So, you're using a compiler in beta testing and an outdated one with an out-dated ide?

Sounds like a recipe for frustration.

For visual studio, the input file should indeed be in the project directory (not to be confused with the solution directory.) For instance, if you started a project named "junk" and visual studio thoughtfully opened a solution named "junk" to put your project in the input file would need to be in the /junk/junk directory. The first is the solution name, the second is the project name.

I haven't tested this with the beta version, so it's possible things have changed, but it doesn't seem likely.

How would i go about using full path?


My VS projects are in "C:/Projects/"

file.open("C:/Projects/Grades/Grades/Grades_Spring2012.txt");

Would be the full path for me if the file was in the working directory for the grades project.

You can use backslashes, if you prefer, but keep in mind they must be escaped.

file.open("C:\\Projects\\Grades\\Grades\\Grades_Spring2012.txt");

Btw, check to see the file is actually named Grades_Sprint2012.txt and not Grades_Sprint2012.txt.txt. Windows likes to trick people by not showing file extensions.





Last edited on
The last thing you said was it.

.txt.txt

It is working now, thank you so much. Greatly appreciated.
Topic archived. No new replies allowed.