Using fstream inside a class and calling from/to main

Hi all,
Iam trying to create a class,say, "example" and some function in it. the goal of the program is to read a array from a file and output the array elements into the another file. File contains integer data. I have given the code I tried so far. Please help me to solve it. Im just the beginner in C++. so your help and suggestions required a lot. Thank you

Header code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<fstream>
using namespace std;
class example
{
public:
	void intialize_array(ifstream,int);
	void output_array();
	ifstream in;
	ofstream out;
	void file_open(ifstream,ofstream);
	void file_close(ifstream,ofstream);
	example();
	int numbers[5];
	int i;
};



implementation file
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
#include"example.h"
#include<fstream>
using namespace std;

void example::file_open(ifstream in,ofstream out)
{
	in.open("example_input.txt");
	out.open("example_output.txt");
}

void example::file_close(ifstream in,ofstream out)
{
	in.close();
	out.close();
}

void example::intialize_array(ifstream in,int num)
{
	for(int i=0;i<5;i++)
	{
		in>>num;
		numbers[i]=num;
	}
}

void example::output_array() 
{
	for(int i=0;i<5;i++)
	{
		out<<numbers[i];
		out<<endl;
	}
}

example::example()
{
	for(int i=0;i<5;i++)
	{
		numbers[i]=1;
	}
}



Test code file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include"example.h"
#include<fstream>

using namespace std;

int main()
{
	ifstream in;
	ofstream out;
	int num;
	
	example ex1;
	ex1.file_open(in,out);
	ex1.intialize_array(in,num);
	ex1.output_array();
	ex1.file_close(in,out);

	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
void example::file_open(ifstream in,ofstream out)
{
	in.open("example_input.txt");
	out.open("example_output.txt");
}

void example::file_close(ifstream in,ofstream out)
{
	in.close();
	out.close();
}


The underlined parameters are passed by value, so they have no effect on the arguments passed because it does not return them. To solve this, either you pass them by reference, or you return them.

For me to help you better, please post your errors.
Last edited on
1
2
3
4
5
void example::file_open(ifstream in,ofstream out)
{
	in.open("example_input.txt");
	out.open("example_output.txt");
}


maybe your example::in is being hidden with ifstream in parameter, and so ofstream out
In addition to what Chipp said, stream types cannot be passed by value, which is likely the source of your compilation errors.
Hi Aceix,

This is the error im getting

Error	1	error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'	c:\program files\microsoft visual studio 10.0\vc\include\fstream	1116


Error	2	error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'	c:\program files\microsoft visual studio 10.0\vc\include\fstream	890



Hi chipp and cire

So should i pass the fstreams as reference(ifstream&,ofstream&)?
So should i pass the fstreams as reference(ifstream&,ofstream&)?


If your intention was to use the stream objects you're storing in the class, you shouldn't be passing them at all.
If your intention was to use the stream objects you're storing in the class, you shouldn't be passing them at all.


that means, you don't need to use another fstream, instead, just use your fstream object member

CMIIW
chipp wrote:
that means, you don't need to use another fstream, instead, just use your fstream object member

CMIIW

Yeah that's true.
oh ok. I understood little bit (because im a just a beginner). I try ti implement it and let u guys know.

Thanks again.
Topic archived. No new replies allowed.