Hi,
My coding objective is quite simple: design a function to write the data in a matrix into files. And here is my code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int matwrite(float* mp,int row_sz,int col_sz,string mat_file);

int main()
{
	// create a simple binary file first for later matrix operation.
	float myMat[3][3] = {1,2,3,4,5,6,7,8,9};
	string file_name = "mat.bin";
	matwrite(myMat,3,3,file_name);	
}
int matwrite(float* mp,int row_sz,int col_sz,string file_name)
{
    ofstream mat_file(file_name.c_str(),ios::binary);
	for (int row=0; row<row_sz; row++)
	{
		for (int col=0; col<col_sz; col++)
		{
			mat_file.write(reinterpret_cast<char*>(&mp[row][col]),sizeof(float));
		}
	}
	cout<<"The matrix has been successfully written to file."<<endl;
	return 0;
}

Errors occured as following during compilation:

e:\code zone\practice code\matrix_class\matrix\matrix_class\mat_demo.cpp(14) : error C2664: 'matwrite' : cannot convert parameter 1 from 'float [3][3]' to 'float *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

e:\code zone\practice code\matrix_class\matrix\matrix_class\mat_demo.cpp(23) : error C2109: subscript requires array or pointer type

Any one can tell me what's the matter and tell me how to fix it? Thank u in advance.

Nicely pointed by Siddhant3s.Whenever you pass an array to a function then its bounds should be clearly defined.The index indicating a pointer may be left as such but the array bounds should be specified as:

float mp[][3];

since the file is in binary mode you can write the matrix in one big swoop instead of one element at a time mat_file.write(reinterpret_cast<char*>(mp), row_sz * col_sz * sizeof(float)); With that you can delete that entire loop. And read it back with that same line, except change write to read.

Thank you all,
After some modificaiton my code is as following and it works well:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int matwrite(float (*mp)[3],int row_sz,int col_sz,string mat_file);

int main()
{
	// create a simple binary file first for later matrix operation.
	float myMat[3][3] = {1,2,3,4,5,6,7,8,9};
	string file_name = "mat.bin";
	matwrite(myMat,3,3,file_name);	
}
int matwrite(float(*mp)[3],int row_sz,int col_sz,string file_name)
{
    ofstream mat_file(file_name.c_str(),ios::binary);
	mat_file.write(reinterpret_cast<char*>(mp),row_sz * col_sz * sizeof(float));
	cout<<"The matrix has been successfully written to file."<<endl;
	return 0;
}

However, this success seems to be trivial since the number of columns has to be set during function definition. So is there any potential to obtain more flexibility on the size of matrix?How?thank u very much.

also write the values of col_max and row_max so that they can be read back at runtime

mat_file.write(reinterpret_cast<char*>(&row_sz), sizeof(int));
mat_file.write(reinterpret_cast<char*>(&col_sz), sizeof(int));
#include <iostream>
#include <fstream>
using namespace std;
#pragma warning(disable: 4996)

int matread(float*& mp,int& row_sz,int& col_sz,string mat_file)
{
    ifstream in(mat_file.c_str());
    in.read(reinterpret_cast<char*>(&row_sz), sizeof(int));
    in.read(reinterpret_cast<char*>(&col_sz), sizeof(int));
    mp = new float[row_sz*col_sz];
    
	in.read(reinterpret_cast<char*>(mp),row_sz * col_sz * sizeof(float));
    return 0;
}

int main()
{
   float *ay = NULL;
   int row_sz = 0, col_sz = 0;
   matread(ay,row_sz, col_sz, "filename.txt");

}
This article has been dead for over six months. Start a new discussion instead.