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:

#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

Try this:

#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 << "]\n";
}


// 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;
    int y;
	char *out_name = "test.bin";
	out_file.open(out_name,ios::binary);

	double matrix[2][3] = {1,2,3,4,5,6};
	// write matrix to a disk file.
	for (y=0; y<2; y++)
	{
		for (int x=0; x<3; x++)
		{
			out_file.write(reinterpret_cast<char*>(&matrix[y][x]),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[y][x]),sizeof(double));
		}
		mVect vec(matrix2[y][0],matrix2[y][1],matrix2[y][2]);
		vec.print();
	}
	in_file.close();
}

Thank you Ancient Dragon, I see my mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.