Reading a matrix from a file



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.
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
```
How you do this will depend on a number of factors: since you haven't tried to explain in any detail, I'll assume you know nothing, and just give you the basics.  

  

The easiest way to do this is to read the text file into a single string, and then use the .NET framework to find the match:  


```
Dim text As String = File.ReadAllText("D:\Temp\MyFile.txt")
Dim index As Integer = text.IndexOf("hello")
If index >= 0 Then
   ' String is in file, starting at character "index"
End If
```
  

There are other ways, but that is pretty much the simplest. hello,

there are more possibilities how write output to file. first is by standard methods of jscript by “scripting.filesystemobject”.

var fso = new activexobject(“scripting.filesystemobject”);

var forreading = 1;

var forwriting = 2;

if (fso.fileexists(‘c:\test.txt’))

var f1 = fso.opentextfile… [@cssc](/u/cssc), another option would be to take the regex way.

first, it finds the index of the 5th pipe.

then, it takes the substring until that index.

give this a try:

inputstr.substring(0, regex.matches(inputstr, "\|")(4).index + 1) c = input()  

t = len(c)  

temp = ""  

for i in range(0,len(c)):  

    if t<3:  

        break  

    else:  

        if i in (0,1,t-2,t-1):  

            temp = temp+c[i]  

        else:  

            continue  

print(temp) it's not clear what it is you want as an output. if it's a row vector, there's no concatenation involved, you just simply reshape your matrix:
a = [0 1 1 1;
 1 0 1 0;
 1 0 0 1]
 v = reshape(a, 1, []) %reshape a into one row. since matlab is column major, this is done column-wise you can use the repmat function here
if you have to print the patter n (here 10) times, use the following code:
a = [5 4 3 2 1 0]';
n = 10;
b =repmat(a, n,1) hello, 

this code checks to see if the diagonal elements of a given matrix a (assuming n x n) are larger in magnitude than the sum of the magnitude of the non-diagonal elements in its row.

line by line explanation:

the first line loops through all the rows of a.

 for i = 1:n

the second line is defining j = [1 2 3 ... n], so a vector containing 1..n,
 
 j = 1:n;

the third line is removing the diagonal element, aka, the ith row and this removes the ith column, so the i,i th element,
 
 j(i) = [];

the fourth line takes the absolute value of all the elements in the ith row of a, excluding the diagonal element: a(i,j) = a(ith row, elements 1,2,...,i-1,i+1,...,n) since we removed the ith element from j previously.
 
 b = abs(a(i,j));

the fifth line computes the absolute value of the diagonal element minus the sum of the absolute value of all the other elements in the row. if check is positive, we know the magnitude of the diagonal element was greater than the sum of all the magnitudes of the other elements in its row.
 
 check(i) = abs(a(i,i)) - sum(b);

the sixth line checks if it's less than zero, in which case the diagonal elements magnitude is not greater than or equal to the sum of the magnitudes of the other elements.
 
 if check(i) < 0

if this is the case, the matrix is not strictly diagonally dominant, which the seventh line prints.
 
 fprintf('the matrix is not strictly diagonally dominant at row %2i

',i)

the eighth and ninth lines end the cycle.
 
 end
 end

sorry if this was too much detail,

hope this helps! can you be a little more precise ? does your send message work corretctly if sent from main procedure ? if yes do you uses qmhsndpm ? who is the receiver (call stack entry) (I suppose it’s ‘*’) ? what level in the stack (call stack count)(I suppose it’s 2 ) ?





A message subfile displays the messages from a program message queue — that’s what they’re for. Normally, they’re linked to the name of the program that displays the message subfile (i.e., displays the display file).


Send a message to the program message queue that’s associated with the message subfile.


Tom more information: [c++ friend functions](http://www.tutorialspoint.com/cplusplus/cpp_friend_functions.htm) you write an int, but try to receive a c-string, that can not work.

the radio will not be available (i.e. have a packet received) 99.9% of the time,

introducing a delay and printing something if that is the case is just crazy.

so your code seems to be copy and pasted crap without any understandin…