GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-May-2009, 20:40
nanchuangyeyu nanchuangyeyu is offline
New Member
 
Join Date: Apr 2009
Posts: 15
nanchuangyeyu has a little shameless behaviour in the past
Question

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:
#include <cmath>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

class mVect
{
    private:
        // NO PRIVATE DATA
    public:
        double i, j, k;
        mVect();
        mVect(double,double);
        mVect(double,double,double);
        void print(); // NEEDS EXTRA LIBRARIES
};

// INITIALIZE EMPTY VECTOR
mVect::mVect()
{
    i=0;
    j=0;
    k=0;
}

// OUTPUT THE VECTOR IN THE FORM [i,j,k]
void mVect::print()
{
    cout << "[" <<  i << "," << j << "," << k << "]";
}


// INITIALIZE 2D VECTOR
mVect::mVect(double a, double b)
{
    i=a;
    j=b;
    k=0;
}

// INITIALIZE 3D VECTOR
mVect::mVect(double a, double b, double c)
{
    i=a;
    j=b;
    k=c;
}

void main()
{
	ofstream out_file;
	ifstream in_file;
    
	char *out_name = "test.bin";
	out_file.open(out_name,ios::binary | ios::app);

	double matrix[2][3] = {1,2,3,4,5,6};
	// write matrix to a disk file.
	for (int y=0; y<2; y++)
	{
		for (int x=0; x<3; x++)
		{
			out_file.write(reinterpret_cast<char*>(&matrix[x][y]),sizeof(double));
		}
	}
	out_file.close();
	cout<< "Matrix has been written to test.bin."<<endl;

    // read the matrix from the former file.
	double matrix2[2][3];
	in_file.open(out_name,ios::binary);
	
	for (y=0; y<2; y++)
	{
		for (int x=0; x<3; x++)
		{
			in_file.read(reinterpret_cast<char*>(&matrix2[x][y]),sizeof(double));
			if (y>=1 && x>=1)
			{
				mVect vec(matrix2[x-1][y],matrix2[x-1][y-1],matrix2[x][y-1]);
				vec.print();
			}
		}
	}
	in_file.close();
}

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  
Old 04-May-2009, 23:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Quetions about file I/O from/to a matrix and an advanced vector class.


Quote:
Originally Posted by nanchuangyeyu
...simple program...
CPP / C++ / C Code:
...
    double matrix[2][3] = {1,2,3,4,5,6};
...

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:
    double matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };

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:
Originally Posted by nanchuangyeyu
CPP / C++ / C Code:

    for (int y=0; y<2; y++)
    {
        for (int x=0; x<3; x++)
        {
            out_file.write(reinterpret_cast<char*>(&matrix[x][y]),sizeof(double));
...
In your write statement, the first index is either 0, 1, or 2 and the second index is either 0 or 1.

Quote:
Originally Posted by nanchuangyeyu
...I use VC++ 6.0 if that makes any difference...

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:
    for (int i = 0; i < n; i++) {
        // i is in scope here
    }
    // i is no linger in scope
   
    for (i = 0; i < n; i++) {// This is invalid
        // A different loop using i
    }

A standard C++ compiler will accept the following:
CPP / C++ / C Code:
    for (int i = 0; i < n; i++) {
        // i is in scope here
    }
    // i is no linger in scope
   
    for (int i = 0; i < n; i++) {// This declares a brand new variable named i
        // A different loop using the different i
    }

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:
    int i; // i is in scope for everything after this in this block
...
    for (i = 0; i < n; i++) {
        // A loop using i
    }
.
.
.
    for (i = 0; i < n; i++) {
        // Another loop using the same variable i
    }
.
.
.

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:
        cout << "x = " << x << ", y = " << y << endl;
        cout << ", writing " << matrix[x][y] << endl;
        out_file.write(reinterpret_cast < char *>(&matrix[x][y]),
                       sizeof(double));

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  
Old 05-May-2009, 07:54
nanchuangyeyu nanchuangyeyu is offline
New Member
 
Join Date: Apr 2009
Posts: 15
nanchuangyeyu has a little shameless behaviour in the past

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 GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 19:11.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.