Can anyone solve this: How to add two matrix from .txt file and store the result to another .txt file and print the result in c++
I don’t know everything for certain, but my first thought is to do this:
- Make file readers
- Get the matrices
- Use a For Loop to go through the matrices and add the numbers together. (Might need a double For loop).
- Make a file writer.
- Write to the file.
I have no idea how to do the c++ thing though.
@risobuj,
I won’t post a solution here, but I’ll give you enough to build one on your own:
To be able to handle files in C++, you must include the fstream library.
Then you can declare a file to be read from as ifstream myFile(NameOfYourFile).
If you want to declare a file to write to, you write ofstream myFile(NameOfYourFile)
You then need to check that the file was successfully opened using myFile.is_open().
At the end you need to close your file with myFIle.close().
Reading from a file can be done using the getline(myFile, line) command where line is a string variable.
I think that pretty much covers everything you need to know. If you need any more help, let us know.