Linq Pattern Matching Problem
The code below searches through a file to see if a pattern matches and stores the file offset in the 'result' variable. I then had a for each loop which cycles through the 'result' variable to display the offsets which worked fine ... Untill, I had some further code to read in the file contents at each offset and perform a calculation on each read in and output it in the console.
For some reason the for each loop is only outputing the first calculation, instead of continuing to do all the other offsets.
Any help to why this for each loop is stopping at the first offset?
Thanks in advance
byte[] pattern = 00 00 00 08 00;
byte[] file = "C:\\123.cfg";
var result = Enumerable.Range(0, file.Length - pattern.Length + 1)
.Where(i => pattern.Select((b, j) => new { j, b })
.All(p => file[i + p.j] == p.b))
.Select(i => i + pattern.Length - 1);
foreach (var value in result) {
int startaddress1 = value + 1;
int EndAddress1 = value + 4;
int startaddress2 = EndAddressLong + 1;
int EndAddress2 = EndAddressLong + 4;
MyGlobals.123_filepath = "C:\\123.cfg";
///////////////////////////// Read in the selected //////////////
BinaryReader br = new BinaryReader(File.OpenRead(MyGlobals.123_filepath),
System.Tex开发者_开发问答t.Encoding.BigEndianUnicode);
for (int i = startaddress1; i <= EndAddress2; i++)
{
br.BaseStream.Position = i;
MyGlobals.Hexbytes += br.ReadByte().ToString("X2") + ",";
}
}
I think your problem is with Console.ReadKey()
you will need to press a key in order to continue the foreach
loop
There is only one reason possible to why the loop exits after one iteration:
- result has only one value inside
精彩评论