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.