Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

How to write out a combination of binary and text data, extracting particular records using C# with VS2010 in a console application?

I am in the process of creating a C# console application using VS2010 to read an input file made up of both binary and text data. The objective is to write out all occurrences of 252 byte records that begin with the characters "51ELRC and trailing each of these record, are the characters JJ*.

Do you know how the following code could be modified to accomplish this objective?
The output data can be in the same format as the input data.

I have attached a sample input file.

The output file is defined as:
string goodFilePath = @"U:\BankFolder\bank.txt";

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace ReadBinary
{
    class BinaryFileTest
    {
        static void Main()
        {
            string goodFilePath = @"U:\BankFolder\bank.txt";
            string errorFilePath = @"U:\BankFolder\error.txt";

            if (File.Exists(goodFilePath))
            {
                File.Delete(goodFilePath);
            }

            if (File.Exists(errorFilePath))
            {
                File.Delete(errorFilePath);
            }

            DirectoryInfo parentDirectory = new DirectoryInfo(@"U:\BankFolder");

            foreach (FileInfo file in parentDirectory.GetFiles())
            {
                StreamWriter sw = null;
                StreamWriter sw1 = new StreamWriter(errorFilePath);
                sw = new StreamWriter(goodFilePath);
                ProcessFile(file.FullName, sw, sw1);
                if (sw != null)
                    sw.Close();
            }

            if (File.Exists(errorFilePath))
            {
                if (new FileInfo(errorFilePath).Length == 0)
                {
                    if (File.Exists(errorFilePath))
                    {
                        File.Delete(errorFilePath);
                    }
                }

                else
                {
                    if (File.Exists(goodFilePath))

                        File.Delete(goodFilePath);                  
                }
            }

        }

        private static void ProcessFile(string filePath, StreamWriter sw, StreamWriter sw1)
        {
            int h;
            int j;
            byte[] ByteBuffer = File.ReadAllBytes(filePath);
            byte[] StringBytes = Encoding.UTF8.GetBytes("51ELRC");
            for (h = 0; h <= (ByteBuffer.Length - StringBytes.Length); h++)
            {
                if (ByteBuffer[h] == StringBytes[0])
                {
                    for (j = 1; j < StringBytes.Length && ByteBuffer[h + j] == StringBytes[j]; j++) ;
                    if (j == StringBytes.Length)
                        Console.WriteLine("String was found at offset {0}", h);
                }
            }
        }
    }
}
xxx
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

The List<byte[]> result contains the 4 sequences of 252 bytes from the txt file:

var anchor1 = Encoding.UTF8.GetBytes("51ELRC");
            var anchor2 = Encoding.UTF8.GetBytes("JJ*");
            List<byte[]> result = new List<byte[]>();
            var bytes = File.ReadAllBytes(@"c:\temp\xxx.txt");
            for (int i = 0; i < bytes.Length; i++)
            {
                var data = bytes.Skip(i).Take(anchor1.Length);
                if (data.SequenceEqual(anchor1))
                {
                    for (int j = i + anchor1.Length; j < bytes.Length; j++)
                    {
                        var data2 = bytes.Skip(j).Take(anchor2.Length);
                        if (data2.SequenceEqual(anchor2))
                        {
                            var res = bytes.Skip(i).Take(j - i).ToArray();
                            result.Add(res);
                            i = j;
                            break;
                        }
                    }
                }
            }
        }

Open in new window

Avatar of zimmer9

ASKER

Thanks for your suggestion sedgwick.

Do you know how I can resolve the following:

Error      1      'System.Array' does not contain a definition for 'Skip' and no extension method 'Skip' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)      U:\Practice\ASP.NET\ConsoleApplication4\ConsoleApplication4\Program.cs      69
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zimmer9

ASKER

One last question. How would you resolve:

Error      1      A local variable named 'j' cannot be declared in this scope because it would give a different meaning to 'j', which is already used in a 'parent or current' scope to denote something else      U:\Practice\ASP.NET\ConsoleApplication4\ConsoleApplication4\Program.cs      73      30      ConsoleApplication4
change j variable to something else to avoid ambiguity error.