data = rand(8, 10);
fid = fopen('File.data', 'w');
if fid == - 1
error('Cannot open file for writing');
end
fwrite(fid, ndims(data), 'uint16');
fwrite(fid, size(data), 'uint64');
fwrite(fid, data, 'double');
fclose(fid);
The reading from C++ is equivalent: Read the number of dimensions as uint16, then the dimension itself as uint64 and then the actual data.