-1

i get index out of bound on line : stream.Read(fileBytes[i], 0, fileBytes[i].Length); any help would be appreciated . thanks :)

string[] path = new string[15];
byte[][] fileBytes = new byte[10][];
for (int i = 1; i <= 10; i++) {

 path[i] = @ "C:\Users\t-chkum\Desktop\InputFiles\1MB\" + i + ".txt ";

 // readind data/* 
 FileStream stream = File.OpenRead(path[i]);
 fileBytes[i] = new byte[stream.Length];

 //  Console.WriteLine(stream.Length);
 stream.Read(fileBytes[i], 0, fileBytes[i].Length);
 stream.Close();
}
Doruk Eren Aktaş
  • 2,121
  • 8
  • 23
kchahat20
  • 97
  • 1
  • 11
  • You JUST asked that question a little bit ago... did you not read the comments? [Reference to your post](https://stackoverflow.com/questions/62179182/index-out-of-bound-c-sharp-arrays) – Jawad Jun 03 '20 at 19:28
  • There was someting wrong with that code. Thats why i asked again after correcting it – kchahat20 Jun 03 '20 at 20:10
  • If there's something wrong with the code in your question, [edit it](https://stackoverflow.com/posts/62179182/edit). Do not repeat your question. – Dour High Arch Jun 03 '20 at 20:47
  • okay will take care of that – kchahat20 Jun 03 '20 at 21:23

1 Answers1

3

You are iterating from i=1 to 10 inclusive. But, the size of the array is 10 i.e. the index goes from 0 to 9. So, either start i from 0, or index using i-1. Also, use try-catch. This would help you to debug better.

Otherwise, the code is working fine for me.

user366312
  • 16,949
  • 65
  • 235
  • 452