static file_header fn_get_file_header(byte[] byte_array) { file_header my_struct = new file_header(); int my_struct_size = Marshal.SizeOf(my_struct); IntPtr my_struct_ptr = Marshal.AllocHGlobal(my_struct_size); Marshal.Copy(byte_array, 0, my_struct_ptr, my_struct_size); my_struct = (file_header)Marshal.PtrToStructure(my_struct_ptr, my_struct.GetType()); Marshal.FreeHGlobal(my_struct_ptr); return(my_struct); }
private void btn_data_converter_start_Click(object sender, EventArgs e) { if (sender != null) { this.Cursor = Cursors.WaitCursor; FileStream data_converter_file_source = null; try { // Open source file data_converter_file_source = new FileStream(g_data_converter_source, FileMode.Open, FileAccess.Read); data_converter_file_source.Seek(0, SeekOrigin.Begin); // Read source file header to get information file_header data_converter_file_info = new file_header(); byte[] tmp_header_byte_array = new byte[Marshal.SizeOf(data_converter_file_info)]; data_converter_file_source.Read(tmp_header_byte_array, 0, Marshal.SizeOf(data_converter_file_info)); data_converter_file_info = fn_get_file_header(tmp_header_byte_array); ushort ai_chnl_cnt = data_converter_file_info.ai_chnl_cnt; double[] ai_chnl_sensitivity = new double[4] { data_converter_file_info.ai_chnl_sensitivity0, data_converter_file_info.ai_chnl_sensitivity1, data_converter_file_info.ai_chnl_sensitivity2, data_converter_file_info.ai_chnl_sensitivity3 }; // Read source file data and then write to a text file StreamWriter data_converter_file_destination = null; try { data_converter_file_destination = new StreamWriter(g_data_converter_destination); double[] data_array = new double[ai_chnl_cnt]; byte[] tmp_data_byte_array = new byte[ai_chnl_cnt * sizeof(double)]; do { int tmp_read_cnt = data_converter_file_source.Read(tmp_data_byte_array, 0, ai_chnl_cnt * sizeof(double)); if (tmp_read_cnt < ai_chnl_cnt * sizeof(double)) { break; } data_array = fn_get_double_array(tmp_data_byte_array); for (ushort vi = 0; vi < ai_chnl_cnt; ++vi) { if (g_data_converter_unit == 1) { // g data_converter_file_destination.Write("{0:f8},", data_array[vi] * 1000 / ai_chnl_sensitivity[vi]); } else { // V data_converter_file_destination.Write("{0:f8},", data_array[vi]); } } data_converter_file_destination.Write("\n"); } while (true); } finally { if (data_converter_file_destination != null) { data_converter_file_destination.Close(); } } } catch (FileNotFoundException) { this.Cursor = Cursors.Default; MessageBox.Show("File not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } finally { if (data_converter_file_source != null) { data_converter_file_source.Close(); } } this.Cursor = Cursors.Default; MessageBox.Show("Data conversion is complete", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } }