ReadAllBytes to fill an array of floats ?

  • Thread starter Thread starter Richard A. DeVenezia
  • Start date Start date
R

Richard A. DeVenezia

Is there a way to read an entire file into a float array ?

Suppose 1,000 floats (arrayed 250 rows by 4 columns) are stored in a
4,000 byte file.

Starting from
byte[] data = File.ReadAllBytes ("floatdump.data");

How would I cast or overlay a float array interpretation of the byte
array ?

Thanks!
 
Richard said:
Is there a way to read an entire file into a float array ?

Suppose 1,000 floats (arrayed 250 rows by 4 columns) are stored in a
4,000 byte file.

Starting from
byte[] data = File.ReadAllBytes ("floatdump.data");

How would I cast or overlay a float array interpretation of the byte
array ?

You can skip the byte array. For an unknown number of floats:

BinaryReader reader = new BinaryReader(new MemoryStream(data));
int floatCount = reader.BaseStream.Length / 4;

float[] floats = new float[floatCount];
for (int i = 0; i < floatCount; i++)
floats = reader.ReadSingle();

reader.Close();

But you can also skip the byte array step:

BinaryReader reader =
new BinaryReader(File.Open("floatdump.data", FileMode.Open));
 
Back
Top