Deleting in the file

I have a code that is generated and placed in input.txt.
In output.txt. modification occurs with these sequences.
But when you open the file again, the old values should be deleted.
That is, I run the code, I receive the input. Then I run the code a second time, and the files should already contain new elements.
In input.txt, this works. And in output.txt, with each new execution of the program and further opening of the file, all elements are placed together with the old ones. What's wrong?

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <iterator>
#include <cmath>
 
double Avarage;
double Avarage1;
double Avarage2;
 
void File(int N, int count){
    std::ofstream out("input.txt");
 
    for (int i = 1; i <= count; ++i) {
        out << rand() % (2 * N + 1) - N << std::endl;
    }
    out << "File is done!\n" << std::endl;
}
 
template <typename Container>
Container inputfile(const std::string& File){
    std::fstream in(File);
 
    Container cont;
 
    if (!in.is_open()) std::cout << "Cannot open file!\n";
    else {
        for (int x{}; in >> x; ) {
            cont.push_back(x);
        }
    }
    return cont;
}
 
template <class T>
void Add_Sum_Avarage(T& conteiner) {
    std::ofstream out("output.txt", std::ofstream::app);
    
    typename T::iterator it0;
    typename T::iterator it2;
    double S = 0;
    double N = 0;
 
    for (it0 = conteiner.begin(); it0 != conteiner.end(); ++it0) {
        S += *it0;
        N++;
    }
 
    conteiner.push_back(S);
    conteiner.push_back(S / N); 
    for (it2 = conteiner.begin(); it2 != conteiner.end(); ++it2)  out << *it2 << std::endl;
    out << "Sum of container = " << S << std::endl; //сумма всех чисел в контейнере
    out << "Average of container = " << (S / N) << std::endl; //среднее арифметичиское всех чисел в контейнере
}
 
template <class T>
T& Modify(T& container) {
    std::ofstream out("output.txt", std::ofstream::app);
    
    out << "\nModify:" << std::endl;
    typename T::iterator it1;
    typename T::iterator it2;
    double MAX = 0;
    double MIN = 0;
 
    for (it1 = container.begin(); it1 != container.end(); ++it1)
        if (MAX < *it1) MAX = *it1; out << "MAX = " << MAX << std::endl; //поиск максимального числа в контейнере
    for (it2 = container.begin(); it2 != container.end(); ++it2)
        if (MIN > *it2) MIN = *it2; out << "MIN = " << MIN << std::endl; //поиск минимального числа в контейнере
 
    double Avarage = (MAX + MIN) / 2.0;
    out << "Avarage of max and min = " << Avarage << std::endl;
    for (const auto& i : container) out << i + std::abs(Avarage) << std::endl;
 
    return container;
}
 
template<class T>
T& Modify2(T& conteiner, typename T::iterator& it3, typename T::iterator& it4) {
    std::ofstream out("output.txt", std::ofstream::app);
    
    out << "\nModify(iterator):" << std::endl;
    double MAX = 0;
    double MIN = 0;
 
    for (it3 = conteiner.begin(); it3 != conteiner.end(); ++it3)
        if (MAX < *it3) MAX = *it3;   out << "MAX = " << MAX << std::endl;//поиск максимального числа в контейнере
    for (it4 = conteiner.begin(); it4 != conteiner.end(); ++it4)
        if (MIN > *it4) MIN = *it4;  out << "MIN = " << MIN << std::endl;//поиск минимального числа в контейнере
 
    double Avarage = (MAX + MIN) / 2.0; 
    out << "Avarage of max and min = " << Avarage << std::endl;
    for (const auto& i : conteiner) out << i + std::abs(Avarage) << std::endl;
 
    return conteiner;
}
 
 template <class T>
 void outfile(T& Container) {
     std::ofstream out("output.txt", std::ofstream::app);
    
     typename T::iterator it3;
     for (it3 = Container.begin(); it3 != Container.end(); ++it3) {
         out << *it3 << std::endl;
     }
     std::cout << "File output" << std::endl;
 }
 
 int Random() { return (std::rand() % 51); }
 double AvarageSum1(double& i1) { return i1 += Avarage1; }
 double AvarageSum2(double& i2) { return i2 += Avarage2; }
 
int main(){ 
    std::ofstream out("input.txt", std::ofstream::app);
    std::ofstream out2("output.txt", std::ofstream::app);
 
    srand(signed (std::time(0)));
 
    File(50, 5);     
    
    std::cout << "Making of container..." << std::endl; 
    std::vector<double> V = inputfile<std::vector<double>>("input.txt");
    std::deque<double> D = inputfile<std::deque<double>>("input.txt");
    std::list<double> L = inputfile<std::list<double>>("input.txt");
    
    std::cout << "Changing of container..." << std::endl;   
    std::deque<double>::iterator itt3;
    std::deque<double>::iterator itt4;
    Add_Sum_Avarage(D);
    Modify(D);
    Modify2(D, itt3, itt4); 
    
 
    std::cout << "Making and changing  of container with the help of methods" << std::endl;
    std::vector<double> v_each (5);
    std::vector<double>::iterator pos1;
    out << "Generate()" << std::endl;
    std::generate(v_each.begin(), v_each.end(), Random);
    bool flag1 = false;
    for (pos1 = v_each.begin(); pos1 != v_each.end(); ++pos1) { 
        auto minmax = std::minmax_element(v_each.begin(), v_each.end());
        Avarage1 = ((*minmax.first + *minmax.second) / 2.0);
        if (!flag1) {
            out << "MAX = " << *minmax.second << std::endl;
            out << "MIN = " << *minmax.first << std::endl;
            out << "Avarage = " << Avarage1 << std::endl;
            flag1 = true;
        }
        out << *pos1 << std::endl;
    }   
    out2 << "\nChanging  of container with the help of method for_each()" << std::endl;
    std::for_each(v_each.begin(), v_each.end(), AvarageSum1);
    outfile(v_each);
 
 
    std::list<double> v_transform (5);
    std::list<double>::iterator pos2;
    out << "\nGenerate()" << std::endl;
    std::generate(v_transform.begin(), v_transform.end(), Random);
    bool flag2 = false;
    for (pos2 = v_transform.begin(); pos2 != v_transform.end(); ++pos2) {
        auto minmax = std::minmax_element(v_transform.begin(), v_transform.end());
        Avarage2 = ((*minmax.first + *minmax.second) / 2.0);
        if (!flag2) {
            out << "MAX = " << *minmax.second << std::endl;
            out << "MIN = " << *minmax.first << std::endl;
            out << "Avarage = " << Avarage2 << std::endl;
            flag2 = true; 
        }
        out << *pos2 << std::endl;
    }   
    out2 << "\nChanging  of container with the help of method transform()" << std::endl;
    std::transform(v_transform.begin(), v_transform.end(), v_transform.begin(), AvarageSum2);
    outfile(v_transform);
}


Last edited on
1
2
3
4
int main(){ 
    std::ofstream out("input.txt", std::ofstream::app);
    std::ofstream out2("output.txt", std::ofstream::app);
 

You need to examine in detail just how many times you're opening files for output.

You have multiple file opens scattered all over the place.
You need to examine in detail just how many times you're opening files for output.

You have multiple file opens scattered all over the place.


I just dont't know how to fix it
Well it's a downside of writing 200 lines of code at once, only to discover that it doesn't actually work.

Start with
1
2
3
4
5
6
7
8
int main(){ 
    std::ofstream out("input.txt", std::ofstream::app);
    std::ofstream out2("output.txt", std::ofstream::app);
 
    srand(signed (std::time(0)));
 
    File(50, 5);     
}

Observe the result.

Then add a few lines at a time until you notice that it's no longer working as you expect.

When expectation != reality, you've found a bug.
Observe the result.

Then add a few lines at a time until you notice that it's no longer working as you expect.

When expectation != reality, you've found a bug.


Thank you very much.
I deleted ::app in 42 line and It has been worked.
Topic archived. No new replies allowed.