1

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.

8
  • 3
    First tip: File.ReadAllBytes is 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 initialized savg1... 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 of savg is 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 / 2 Commented Sep 15, 2016 at 12:26
  • fileBytes.Length - 32 why -32? Commented Sep 15, 2016 at 12:28
  • BitConverter.ToInt16 and double 16bit & 64bit... hu? Commented Sep 15, 2016 at 12:30
  • declaration of savg1 is : double[] savg1 = new double[1000000]; when alg run: line int length = (int)b.BaseStream.Length; the length value assigned to 24000000 i do not no why! Commented Sep 15, 2016 at 12:33
  • i do not know why -32! Commented Sep 15, 2016 at 12:35

2 Answers 2

1

I suggest something like this (File.ReadAllBytes and BitConverter.ToDouble):

  byte[] source = File.ReadAllBytes(@"C:\Users\Hanieh\Desktop\nums.txt");
  double[] data = new double[source.Length / sizeof(double)]; 

  for (int i = 0; i < data.Length; ++i)
    data[i] = BitConverter.ToDouble(source, i * sizeof(double));
Sign up to request clarification or add additional context in comments.

Comments

0

I would solve it like:

double[] data;

using (BinaryReader b = new BinaryReader(File.Open("C:\\Users\\Hanieh\\Desktop\\nums.txt", FileMode.Open)))
{
    // create array/buffer for the doubles  (filelength / bytes per double)
    data = new double[b.BaseStream.Length / sizeof(double)];

    // read the data from the binarystream
    for (int i = 0; i < data.Length; i++)
        data[i] = b.ReadDouble();
}

MessageBox.Show("doubles read: " + data.Length.ToString());

Although, your file nums.txt implies that it is a textfile. You might not read it as a binary file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.