![]() |
|
#1
|
|||
|
|||
Reading A matrix From a .dat fileI am looking to create a C++ code that will first open and read a .dat file called shms.dat. This file is a first order 6 X 6 matrix, which is set up as follows:
1st row -1.38013 0.00000 0.00000 0.00000 0.00000 1.65616 2nd row -0.57498 -0.72457 0.00000 0.00000 0.00000 3.23192 3rd row 0.00000 0.00000 -1.63518 0.00000 0.00000 -0.15092 4th row 0.00000 0.00000 -2.71347 -0.61155 0.00000 0.07025 5th row 0.35082 -0.12000 0.05244 0.00923 1.00000 -0.05134 6th row 0.00000 0.00000 0.00000 0.00000 0.00000 1.00000 Now, the code ideally is to do 2 things: it is supposed to first read in the shms.dat file as a first order 6 X 6 matrix and writes it back out as a 6 X 6 matrix. Then, it is supposed to create an input vector that can be multiplied by this matrix after it is read in. Now, I do have some idea of where to start; I am pretty sure the best way to do this in C++ is to use the fstream class. I have tried streaming the file and then reading it and writing it out; the code is as follows, so as to show what I've tried to do and perhaps more importantly that I've tried to work on this myself before asking someone else for insight: CPP / C++ / C Code:
The error messages I get when I run it are: Code:
Now, some of these error messages are rather peculiar, since I thought, for instance, the classes ifstream and fstream were inherent in C++. Now it seems that is not the case. In any event, I was trying above to do the first part of the code-read in and write out a first order 6 X 6 matrix and write it out. Then, I was looking to multiply and write (unless most C++ users prefer to call it "print") out the multiplication results. Thank you very much in advance for any type of help you can provide. Last edited by admin : 03-Aug-2008 at 19:39.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|||
|
#2
|
|||
|
|||
Re: Reading A matrix From a .dat fileQuote:
However, I respectfully suggest that you look for examples that use this class. Look in a book or find some explanation and examples on the web. For example: cplusplus.com files tutorial. There are lots of others. Poke around on the web until you find one that you like. Look at their examples. Use the header files that they show. Then, maybe, if it's not exactly what you need, you will be starting from something that works. Their examples use the getline function for reading, and that's the way that I might do it. For getting started, using the overloaded >> operator works, too, and is entirely appropriate for many programs such as yours. For example, here's my take on the first part of your problem: CPP / C++ / C Code:
Key features: Always test to make sure the file opened properly. Test the condition of the stream after numeric inputs to make sure it's OK. Be verbose in user output messages. For example, if there are input errors, I never just say "program terminating," I tell the user exactly what happened that caused the problem. There are a lot of ways to get the program to ignore the first two "things' on a line so that you can get to the numeric stuff. Your way of reading something into a "dummy" variable that you then throw away is very practical. The thing is, that since the first two "things" are not numeric quantities, you can't read them into integer data types. I used a std::string dummy variable; you can do other things as well I also show some elementary output formatting so the output is lined up neatly in columns. For your program to write the matrix values to a file, you would open an ofstream and instead of "cout << ", you would use the "<<" operator with the outpug stream. Here's the output from my test program: Code:
Note that I would probably comment out the "line x First item..." stuff after I had made sure that the program reads exactly the number of lines and data items that the file contains. The point is that, especially with new programs, I make the program tell me exactly what it is doing at each step. (The silliness of "line 0" is due to the fact that I just printed out the value of i and it starts from zero. Since that's only there during initial program testing, I didn't try to "dress it up" by making the line numbers go from 1 to 6. Chacun à son goût...) Regards, Dave Footnote: It's C++. Although it is "legal" to use C library functions (including the FILE stuff from <stdiio.h>), there are many advantages to using C++ streams. Generally speaking, I think that most C++ programmers don't like to use both types in the same program. My advice: If you are learning to use C++, then use the functions and features of C++. (That's only an opinion, however, and it is worth exactly what you want it to be worth.) Last edited by davekw7x : 03-Aug-2008 at 15:00.
|
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| contents of .txt file into 2D array | anirudhroxrulz | C Programming Language | 4 | 10-Apr-2008 22:45 |
| Need help - complex matrix, complex vector | mosquito | C++ Forum | 30 | 10-Apr-2008 03:16 |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| Reading Matrix from Text File | TheHive | C Programming Language | 3 | 30-Aug-2006 17:55 |
| After execution - Error cannot locate /Skin File? | WSCH | C++ Forum | 1 | 05-Mar-2005 20:03 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The