troubleshooting Question

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

Avatar of zimmer9
zimmer9Flag for United States of America asked on
.NET ProgrammingC#
5 Comments1 Solution457 ViewsLast Modified:
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros