C++ to C# Reading Binary File into a two dimensional float array -
i have been assigned convert c++ app c#.
i want convert following code in c# rate_buff double[3,9876] 2 dimensional array.
if ((fread((char*) rate_buff, (size_t) record_size, (size_t) record_count, stream)) == (size_t) record_count)
if correctly guessed requirements, want:
int record_size = 9876; int record_count = 3; double[,] rate_buff = new double[record_count, record_size]; // open file using (stream stream = file.openread("some file path")) { // create byte buffer stream reading record_size * sizeof(double) in bytes byte[] buffer = new byte[record_size * sizeof(double)]; (int = 0; < record_count; i++) { // read 1 record if (stream.read(buffer, 0, buffer.length) != buffer.length) throw new invaliddataexception(); // copy doubles out of byte buffer 2 dimensional array // note assumes machine-endian byte order (int j = 0; j < record_size; j++) rate_buff[i, j] = bitconverter.todouble(buffer, j * sizeof(double)); } } or more concisely binaryreader:
int record_size = 9876; int record_count = 3; double[,] rate_buff = new double[record_count, record_size]; // open file using (binaryreader reader = new binaryreader(file.openread("some file path"))) { (int = 0; < record_count; i++) { // read doubles out of byte buffer 2 dimensional array // note assumes machine-endian byte order (int j = 0; j < record_size; j++) rate_buff[i, j] = reader.readdouble(); } }
Comments
Post a Comment