![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Questions about file I/O from/to a matrix and an advanced vector class.Hi,
I am now writing a simple program for testing file I/O and an advanced vector class. Unfortunately I have encountered some confusing problems. My code is as following: CPP / C++ / C Code:
It can be compiled without any error,but the result is not OK. I want to write the matrix {1,2,3;4,5,6} to the file test.bin. But when viewed under data viewer software,the resulting file is : 0000000000 1.0000000000 4.0000000000 0000000016 0.0000000000 2.0000000000 0000000032 5.0000000000 0.0000000000 Finally, I want to read the file "test.bin" again into matrix2 and for some proper elements of matrix2,say,matrix2[x][y], to initialize an mVec vector with its context vector(matrix2[x-1][y],matrix[x-1][y-1],matrix[x][y-1]),but there is a runtime error. In "debug step by step"mode the error reads: " Unhandled exception in Advanced Math Vector Class.exe:0xC0000005:Access Violation". Anybody can tell me why all these are happening and give me some advice for an efficient solution?Thank u in advance.(I use VC++ 6.0 if that makes any difference.) nanchuangyeyu |
|||
|
#2
|
|||
|
|||
Re: Quetions about file I/O from/to a matrix and an advanced vector class.Quote:
To access the elements of that matrix, the first index can be either 0 or 1. The second index can be 0, 1, or 2. In matrix terminology, the array has two rows and three columns. By the way, the proper way to initialize this is CPP / C++ / C Code:
Leaving out the inner brackets earns a warning on some compilers, but the actual result is the same as it would be with your initializer statement. The elements are stored contiguously in the order specified. Here's the thing that is emphasized by the proper notation: The so-called 2D array is actually an array of arrays. For this example, there are two arrays, and each array has three elements matrix[0] is an array of three doubles. The elements are matrix[0][0] = 1 matrix[0][1] = 2 matrix[0][2] = 3 matrix[1] is an array of three doubles. The elements are matrix[1][0] = 4 matrix[1][1] = 5 matrix[1][2] = 6 However, in your program... Quote:
Quote:
Actually it does. 1. Standard C++ compilers require int main and not void main. I strongly recommend that you get into the habit of using standard declarations. Some other compilers are more nearly Standards-compliant and won't even compile this program.. You might not be using VC++6 forever, you know. 2. Standard C++ compilers operate like this: If a variable is declared as part of the for() statement, it is in scope only in that loop. So with a standard C++ compiler the following won't compile: CPP / C++ / C Code:
A standard C++ compiler will accept the following: CPP / C++ / C Code:
Since Visual C++ Version 6 won't accept the second one, you can use something like the following, which can be used with VC++6 as well as all standard C++ compilers CPP / C++ / C Code:
This form is standard C++ and is also accepted by Visual C++ version 6. See Footnote. Bottom line: By using an old, non-standard compiler you are writing programs that are not correct according to the C++ Language specification. There are certain other "features" of that compiler and its libraries that are not standard. There are a number of known bugs in the compiler, libraries and headers that will never be fixed in that compiler because Microsoft stopped supporting and updating it some few years ago. That doesn't mean that you can't use Visual C++ version 6 to learn C++, but you will have to make adjustments "some day" when you expand your horizons. You see that the file doesn't contain what you expect, so how about making the program tell you what it is writing. Maybe something like: CPP / C++ / C Code:
Regards, Dave Footnote: My second loop example (the one that works with VC++6 as well as standard compilers) is not necessarily the "best" style according to some people, since they prefer variables like loop counters be declared in that loop. It's a matter of style, however, and my last loop example is perfectly "legal." (And it will work with VC++6 as well as all other C++ compilers.) Some day you may encounter a professor or a boss who requires one or the other as part of some local "programming style." You should be familiar with both. |
|
#3
|
|||
|
|||
Re: Quetions about file I/O from/to a matrix and an advanced vector class.Thank you very much for your generous help dave, I have really learned something useful from your introduction. I will do better.
nanchuangyeyu |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help - complex matrix, complex vector | mosquito | C++ Forum | 30 | 10-Apr-2008 04:16 |
| i need help in C++ PLZ | its_me | C++ Forum | 3 | 04-Dec-2006 22:51 |
| Reading Matrix from Text File | TheHive | C Programming Language | 3 | 30-Aug-2006 18:55 |
| Combining Vectors and References | Frankg | C++ Forum | 7 | 14-Jan-2006 07:17 |
| saving matrix from file to array | amina17 | C Programming Language | 5 | 03-Jun-2005 13:04 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The