reading and writing to file

Hello, I am a beginner and need some urgent help. I will really appreciate if anyone can refer me to the specific topics which I should read to comprehend the topics that I am gonna ask.

This is I need to do-
1. I am working with complex numbers (real and imaginary). The input file will contain an array like this:

R0 I0 R1 I1 R2 I2 R3 I3

where each pair (R I) represents the real and imaginary values of each complex numbers. I know how to create my own data type for complex numbers and I already did that. Now, I want to create an array "a" of complex numbers where a[0]=R0 +i I0, a[1]= R1 +i I1 and a[2]=R3 +i I3. The values are required to be read from text file.
How can I implement that?

Thanks in advance. I didn't do file operations before. So, it will be really helpful to get some help. I would really appreciate if atleast anyone can refer me to the class or functions related to file op which will be necessary to do this.

I think the easiest way to pair real and imaginary numbers is to make a structure:

1
2
3
4
5
struct RI
{
    double real;
    double imaginary;
};


Then to make an array, just do this:
RI MyRIarray[10];

To access each member:
1
2
MyRIarray[0].real = 4.5;
MyRIarray[0].imaginary = -3.1;



For file I/O stuff use #include <fstream> .
To read look up the stuff and examples here:
http://cplusplus.com/reference/iostream/ifstream/
To write, look up the stuff and examples here:
http://cplusplus.com/reference/iostream/ofstream/

There's also a tutorial here:
http://cplusplus.com/doc/tutorial/files/
i use this :

first include fstream

then use this code for input :

ifstream fin("file name.what ever it is");


and for output :

ofstream fout("filename.what ever it is");

then just remmember to use fin for input and fout for out put for example:

fin >> a;
fout<<"Hello DUDE!";
Thanks for the help. I did some coding for my datatype.
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
class complex
{
private:
	float a,b;   // a=real, b=imaginary
public:
	complex(float x=0, float y=0)
	{
		a=x; 
		b=y;
	}
	complex operator+ (complex x)
	{
		complex y;
		y.a = a + x.a;
		y.b = b + x.b;
		return y;
	}
	complex operator* (complex x)
	{
		complex y;
		y.a = (a*x.a) - (b*x.b);
		y.b = a*x.b + b*x.a;
		return y;
	}
	void print()
	{
		cout << a << " " << b << " ";
	}
	void takeinput()
	{
			cin >> this->a;
			cin >> this->b;
	}
};

void main ()
{
	complex c[4];
	for (int i=0; i<=3; i++)
	{
		c[i].takeinput();
	}

	for (int j=0; j<=3; j++)
	{
		c[j].print();
	}
}


Here, I can declare arrays of complex number and I can take the respective values from user. BUT my problem is, I have a text file like that:

0 1 2 3 3 4 3 5 4

Here, the complex numbers are c[0].real=1, c[0].img=2, c[1].real=3, c[1].img=3, c[2].real=4, c[2].img=3, c[3].real=5, c[3].img=4

How can I do that? I know how to read from file and how to store values in file. However, in this case, I need to know what integer from the file I am reading. Because the first integer (0) is not a part of complex numbers. Starting from 1, each pair will represent one complex number. How can I implement that? It seems that I need to run a loop and whenever I came across an integer in the file, I would need to store it in appropriate location in my array.


Thanks in advance.
Note: C++ has support for complex types:
http://www.cplusplus.com/reference/std/complex/
;)
Add this to your class in the private section:
1
2
ifstream fin("input.txt");
ofstream fout("output.txt")


In complex::takeinput() change cin to fin.
In complex::print() change cout to fout.

If you want to get rid of that pesky 0. then make another function specifically for this in the class:
1
2
3
4
5
void waste_input()
{
    double temp;
    fin >> temp;
}


Then just call that before you enter that first for loop.
Topic archived. No new replies allowed.