Deleting in the file.

In the code, I have described the File (), Modify () and Add_Sum_Avarage () functions. File () must fill the file of the number, Modify () does some modification (mine multiplies by -1, if the number is negative), Add_Sum_Avarage () calculates the sum of the entire sequence, finds the arithmetic mean (these numbers are added to the end of the container), finds the maximum and minimum and calculates the arithmetic mean between them, then the average is added to each element.

The problem is that when in main I write first
1
2
3
4
           File(50, 5);
           std::deque<double> D = inputfile_to_deque();
           Modify(D);
           Add_Sum_Avarage(D);

I got Modify() и Add_Sum_Avarage(). When I write
1
2
3
4
          File(50, 5);
          std::deque<double> D = inputfile_to_deque();
          Add_Sum_Avarage(D);
          Modify(D);

I got only Modify(). Why that? It should give me every function.

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
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <iterator>
#include <cmath>

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!" << std::endl;
}

std::vector<double> inputfile_to_vector() {
    std::vector<double> vec;

    std::fstream in("input.txt", std::fstream::in | std::fstream::out);

    if (!in.is_open()) std::cout << "Cannot open file!\n";
    else {
        for (int x; in >> x; ) {
            vec.push_back(x);
        }
    }
    return vec;
}

std::deque<double> inputfile_to_deque() {
    std::deque<double> deq;

    std::fstream in("input.txt", std::fstream::in | std::fstream::out);

    if (!in.is_open()) std::cout << "Cannot open file!\n";
    else {
        for (int x; in >> x; ) {
             deq.push_back(x);
        }
    }   
    return deq;
}

std::list<double> inputfile_to_list() {
    std::list<double> list;

    std::fstream in("input.txt", std::fstream::in | std::fstream::out);

    if (!in.is_open()) std::cout << "Cannot open file!\n";
    else {
        for (int x; in >> x; ) {
            list.push_back(x);
        }
    }
    return list;
}

template <class T>
T& Modify(T& X) {

    std::ofstream out("input.txt");
    
    typename T::iterator p;
    
    for (auto& i : X) {
        if (i < 0) {
            i *= -1;
        }
        out << i << " ";
    }
    return X;
}

template <class T>
 void Add_Sum_Avarage(T& conteiner) {
    std::ofstream out("input.txt");

    typename T::iterator pos;
    typename T::iterator pos1;
    typename T::iterator pos2;

    int MAX = 0;
    int MIN = 0;    
    int S = 0, N = 0;
    double SA = 0;
    for (const auto& i : conteiner) out << i << std::endl;
    for (pos = conteiner.begin(); pos != conteiner.end(); ++pos) {
        S += *pos;
        SA += *pos;
        N++;
    }   
    conteiner.push_back(S);
    conteiner.push_back(SA / N);
   
    out << "\nSum of container = " << S << std::endl;
    out << "Average of container = " << (SA / N) << std::endl;

    for (pos1 = conteiner.begin(); pos1 != conteiner.end(); ++pos1)
        if (MAX < *pos1) MAX = *pos1; out << "\nMAX = " << MAX << std::endl;

    for (pos2 = conteiner.begin(); pos2 != conteiner.end(); ++pos2)
        if (MIN > * pos2) MIN = *pos2; out << "MIN = " << MIN << std::endl;

    double Avarage = (MAX + MIN) / 2.0;
    out << "Avarage of max and min = " << Avarage << '\n' << std::endl;

    for (const auto& i : conteiner) out << (i + std::abs(Avarage)) << std::endl;    
}

int Random() { return rand() % (2 * 50 + 1) - 50; }

int main(){
    
    srand(time(NULL));
    
    File(50, 5);
    
    std::cout << "Making of container..." << std::endl; 
    std::vector<double> V = inputfile_to_vector();
    std::deque<double> D = inputfile_to_deque();
    std::list<double> L = inputfile_to_list();
    
    std::cout << "Changing of container..." << std::endl;   

    Modify(D);
    Add_Sum_Avarage(D);
}

 
std::ofstream out("input.txt");


This will erase any existing content of the file (truncate).

If you want to append to existing content, you need to open as:

 
std::ofstream out("input.txt", std::ofstream::app);

If you want to append to existing content, you need to open as:

Thank you very much!
@seeplus
std::ofstream out("input.txt", std::ofstream::app);


sorry, do you know how can I add file to the argument of inputfile_to_deque(), inputfile_to_vector(), inputfile_to_list()

I wrote somthing like that:
 
std::deque<int> inputfile_to_deque(std::ofstream& File)


But what do I need to do in main()?

 
std::deque<int> D = inputfile_to_deque(File); //? 
Topic archived. No new replies allowed.