Hello guys,
Firstly I want to apologize If I ask something silly, but I have just started learning C++, and I really don't know much.
As a practice I have a challenge of writing a matrix in a file, and then reading the information (numbers) from it, and in the end to be able to do some calculations, for example to multiply the positive members of that matrix.
So far I have only managed to write a matrix in a file by using this code:
-------
int main()
{
const int m=4, n=5;
int i,j;
int A[m][n]={{5,3,-4,8,2},
{1,5,2,7,-8},
{-3,9,5,2,4},
{4,-1,3,6,8}};
ofstream Write("c:/matrix/matrix.txt", ios::out);
for (i=0;i<m;i++)
{
For (j=0;j<n;j++)
Write << setw(3)
<< A[j] ;
Write << "n";
}
return 0;
}
--------------
Now the next thing i want to do, is to make some calculations with specific members of the matrix, say multiply the negative members or something like this.
I'm trying to use the "ifstream Read("c:/matrix/matrix.txt", ios::in)" to open the file for reading, but I can't figure it out how to achieve the above goal.
Can someone show me an example of how I might achieve this?

Thanks in advance for any kind of help.

Recommended Answers

All 3 Replies

Hi Valdeth. You should use input and output file streams if you want to write and read from a file. Basically you open the file for writing and once you are done close the file. Later you can open the file for reading. The example below reads numbers from the file and just prints them out. If you want to read the file line by line you should look at the getline() function. I have just used your initial array to come up with a result per array which gives the product of all negative numbers in the array. Note that there HAS to be at least 1 negative number per array.

const int m=4, n=5;
	int i,j;
	int num;
	int result;
	int A[m][n]={{5,3,-4,8,2},
				 {1,5,2,7,-8},
				 {-3,9,5,2,4},
				 {4,-1,3,6,8}};

	ofstream outfile;
	ifstream infile; 

	outfile.open("c:/matrix/matrix.txt");

	for (i=0;i<m;i++)
	{
		result = 1;

		for (j=0;j<n;j++)
		{
			outfile << setw(3)<< A[i][j];

                            // Multiply negative numbers in each array (has to be at least 1 negative number in each array)
			if (A[i][j] < 0)              				                      result *= A[i][j];  // result = result x A[i][j]
		}
		cout << "Result of array " << i << " is " << result << endl;
		outfile << "\n";
	}

	outfile.close(); // Close for writing

	infile.open("c:/matrix/matrix.txt"); // opens the file
    
	infile >> num;
	while ( !infile.eof() ) 
	{ // keep reading until end-of-file
		cout << "The next number is " << num << endl;
		infile >> num; // sets EOF flag if no value found
	}
	infile.close();  //Close for reading

I agree with using both an "ifstream" and an "ofstream" because it will simplify the process of reading and writing; if one used "fstream" for both i and o, it will most likely complicate things more.

Thanks for the reply Greg, I will give it a try now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.