I found a C# source code that read from file which have 1000000 double number. The source code is below.
public void filereader()
{
using (BinaryReader b = new BinaryReader(File.Open("C:\\Users\\Hanieh\\Desktop\\nums.txt", FileMode.Open)))
{
int length = (int)b.BaseStream.Length;
byte[] fileBytes = b.ReadBytes(length);
for (int ii = 0; ii < fileBytes.Length - 32 ; ii++)
{
savg1[ii / 2] = (double)(BitConverter.ToInt16(fileBytes, ii) / 20000.0);// inja error index midee
ii++;
}
}
}
when I run source code to read from text file I have an error that related to savg1 index that is out of bound. I debug step by step and result shows size of length= 24000000 but savg1=1000000. my question is here: how this source code work and how I can fix this problem.
File.ReadAllBytesis a simpler way of reading all the bytes in a file... Now, we can't reproduce this as you haven't shown how you've declared or initializedsavg1... it's not clear what you mean by "size of length= 24000000 but savg1=1000000"... if you mean the length of the file is 24000000 but the size ofsavgis 1000000 then that explains the exception... you need to initialize it to the right size, which looks like it needs to be(fileBytes.Length) - 32 / 2fileBytes.Length - 32why-32?BitConverter.ToInt16anddouble16bit & 64bit... hu?