zimmer9
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.RegularExpress ions;
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:\BankFol der");
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).Le ngth == 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("51 ELRC");
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
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.RegularExpress
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:\BankFol
foreach (FileInfo file in parentDirectory.GetFiles()
{
StreamWriter sw = null;
StreamWriter sw1 = new StreamWriter(errorFilePath
sw = new StreamWriter(goodFilePath)
ProcessFile(file.FullName,
if (sw != null)
sw.Close();
}
if (File.Exists(errorFilePath
{
if (new FileInfo(errorFilePath).Le
{
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("51
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
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\Consol eApplicati on4\Consol eApplicati on4\Progra m.cs 69
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\Consol
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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\Consol eApplicati on4\Consol eApplicati on4\Progra m.cs 73 30 ConsoleApplication4
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\Consol
change j variable to something else to avoid ambiguity error.
Open in new window