File is like argument

I have the code and I need to file has been like argument of function:
1
2
3
4
         inputfile_to_vector();
	 inputfile_to_deque();
	 inputfile_to_list();

I tried to do it by yourself, I maked it like comments. What do I need to do in main? Or do I need to do it differently?

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

std::vector<int> inputfile_to_vector(std::ofstream& File) { //here 
	std::vector<int> 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<int> inputfile_to_deque(std::ofstream& File) {  //here 
	std::deque<int> 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<int> inputfile_to_list(std::ofstream& File) {  //here 
	std::list<int> 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", std::ofstream::app);	

	typename T::iterator p;
	
	out << "Modify:" << std::endl;
	for (auto& i : X) {
		if ((((i*(-1)) % 2) == 1) || ((i % 2) == 1)) {
			i += 1;
		}
		out << i << std::endl;
	}
	return X;
}

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

	typename T::iterator it;
	typename T::iterator it1;
	typename T::iterator it2;

	double MAX = 0;
	double MIN = 0;
	double S = 0;
	double N = 0;
	
	for (it = conteiner.begin(); it != conteiner.end(); ++it) {
	    S += *it;
		N++;
	}	
	   
	out << "\nSum of container = " << S << std::endl; в контейнере
	out << "Average of container = " << (S / N) << std::endl; 

	conteiner.push_back(S); 
	conteiner.push_back(S / N);

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

	for (it2 = conteiner.begin(); it2 != conteiner.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 << '\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(){
	std::ofstream out("input.txt");
	srand(time(NULL));
	File(50, 5);     
	
	std::cout << "Making of container..." << std::endl;	
	std::vector<int> V = inputfile_to_vector(/*???*/);
	std::deque<int> D = inputfile_to_deque(/*???*/);
	std::list<int> L = inputfile_to_list(/*???*/);
	
	std::cout << "Changing of container..." << std::endl;	
	Modify(D);
	Add_Sum_Avarage(D);
}
You have functions like:
1
2
3
4
5
6
T function( std::ofstream& out ) { 
  std::fstream in( "something" );
  T vec;
  for ( int x; in >> x; ) vec.push_back( x );
  return vec;
}

* You don't use the out in the function. Why do you pass parameters that you don't use?
* The out is an output stream. The function opens and reads from "something".

1
2
3
4
5
6
7
8
// hard-coded
T function() { 
  std::fstream in( "something" );
  // ...
}

// used:
T result = function();


1
2
3
4
5
6
7
8
// pass name
T function( const std::string& filename ) { 
  std::fstream in( filename );
  // ...
}

// used:
T result = function( "something" );


1
2
3
4
5
6
7
8
// pass stream
T function( std::fstream& in ) { 
  // ...
}

// used:
std::fstream in( "something" );
T result = function( in );
Just pass the stream?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(){
	std::ofstream out("input.txt");
	srand(time(NULL));
	File(50, 5);     
	
	std::cout << "Making of container..." << std::endl;	
	std::vector<int> V = inputfile_to_vector(out);
	std::deque<int> D = inputfile_to_deque(out);
	std::list<int> L = inputfile_to_list(out);
	
	std::cout << "Changing of container..." << std::endl;	
	Modify(D);
	Add_Sum_Avarage(D);
}
What are you trying to do? Within those functions you do not use the output stream. Do you mean you want to pass the input stream? Well, nearly the same. Just change the parameter and line 118 to std::ifstream and remove line 24/38/52
keskiverto

// pass name
T function( const std::string& filename ) {
std::fstream in( filename );
// ...
}

// used:
T result = function( "something" );


something like that?
1
2
3
4
5
6
7
8
9
10
11
12
13
std::vector<int> inputfile_to_vector(const std::string& File) { //here 
	std::vector<int> vec;
	std::fstream in(File);

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

And what do I need to write in main? I wrote:
 
std::vector<int> V = inputfile_to_vector("input.txt");
Last edited on
Topic archived. No new replies allowed.