Why won't it output to textfile

I got it to input the numbers from a text file and i want output the average in another text file but it does not work. However when i output inside c++ its works.

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
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

int main(){


ifstream infile("C:\\infile.txt");
vector<int>vec;
double sum=0;
double average;


if(infile.is_open()){
while(infile.good()){
int line;
infile>>line;
vec.push_back(line);
}
int x=vec.size();

for(int i=0; i<vec.size(); i++){
sum=sum+vec[i];
}

int avg=0;
avg=sum/vec.size();



char lettergrade;
if (avg >= 93){
lettergrade = 'A';
}
else if ((avg >=85) && (avg <93)){
lettergrade = 'B';
}
else if ((avg >=77) && (avg <84)){
lettergrade = 'C';
}
else if ((avg >70) && (avg <76)){
lettergrade = 'D';
}
else {
lettergrade = 'F';
}


infile.close();


fstream outfile("C:\\outfile.txt");
outfile.open("C:\\outfile.txt");


outfile<<" Sum : "<<avg<<" "<<lettergrade<<endl;
cout<<"great sucess";
outfile.close();
cout<<endl;
cout<<endl;
cout<<endl;
}



else{
cout<<"Unable to open the file."<<endl;
}







return 0;

}
btw i also tried removing line 51 it still does not work
This should work: replace from line 58 - 59 with:

1
2
ofstream outfile;
outfile.open("outfile.txt");


Don't know why, just checking my personal notes I see it worked for me before.

Last edited on
tried ofstream too still does not work
But the difference is that when i declare ofstream variable, I don't bind it to any file, it's just

 
ofstream outfile;


instead of:

 
ofstream outfile("filename.txt");
This is what I'm running (and it works):

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
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

int main(){

    ifstream infile("infile.txt");
    vector<int>vec;
    double sum=0;
    double average;

    if(infile.is_open()){
        while(infile.good()){
            int line;
            infile>>line;
            vec.push_back(line);
        }
        int x=vec.size();

        for(int i=0; i<vec.size(); i++){
            sum=sum+vec[i];
        }

        int avg=0;
        avg=sum/vec.size();



        char lettergrade;
        if (avg >= 93){
            lettergrade = 'A';
        }
        else if ((avg >=85) && (avg <93)){
            lettergrade = 'B';
        }
        else if ((avg >=77) && (avg <84)){
            lettergrade = 'C';
        }
        else if ((avg >70) && (avg <76)){
            lettergrade = 'D';
        }
        else {
            lettergrade = 'F';
        }

        infile.close();

        ofstream outfile;
        outfile.open("outfile.txt");

        outfile << " Sum : " << avg << " " << lettergrade << endl;
        cout<<"great sucess";
        outfile.close();
        cout<<endl;
        cout<<endl;
        cout<<endl;
    }
    else {
        cout << "Unable to open the file." << endl;
    }

return 0;
}
but don't you need to specify a location for the file like "C://outfile.txt"?
If you don't, then the file will be created in the same directory where the file with the code lives:

if it's in some place like

C:\User\MyC++Programs\

every needed archive will be looked for or created there, if no relative or absolute path is provided, you can try.
still not working..
mmm... Then I can't help you. I suggest you paste my code and run it .
May be we should go back and try this:

Can you run this code?
Does it work??


1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ofstream test;
    test.open("Testing.txt"); 
    test << "If you see this on the file, this works";
    cout << "Done" << endl;
}
that's not working either
there we go... so does it ouput "done" on the screen?
Do you have permission to create files in the directory where you are trying to?

Now this is more interesting:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ofstream test;
    test.open("Testing.txt");
    if (test.is_open()){
        test << "If you see this on the file, this works";
        cout << "Done" << endl;
    }
    else  {
        cout << "Could not open file Testing.txt" << endl;
    }
    return 0;
}
okay i disabled my anti virus and everything works great
great!!

You should mark this topic as solved.

Topic archived. No new replies allowed.