zimmer9
asked on
How would you modify a routine so that only the first element of an array is written out in a C# console appplication?
I am writing a C# Console application using VS2010.
How would you rewrite the following routine so that only the first element of an array is written out? I only need the first element to be written out. Ideally I just need the 1st string of data to be written out and no longer need an array of elements.
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -----
private static void ProcessFileBinary(string filePath, StreamWriter sw1)
{
var anchor1 = Encoding.UTF8.GetBytes("II *");
var anchor2 = Encoding.UTF8.GetBytes("55 ELRC");
List<byte[]> result1 = new List<byte[]>();
var bytes = File.ReadAllBytes(filePath );
for (int i = 0; i < bytes.Length; i++)
{
var data = bytes.Skip(i).Take(anchor1 .Length);
if (data.SequenceEqual(anchor 1))
{
for (int k = i + anchor1.Length; k < bytes.Length; k++)
{
var data2 = bytes.Skip(k).Take(anchor2 .Length);
if (data2.SequenceEqual(ancho r2))
{
var res = bytes.Skip(i).Take(k - i).ToArray();
result1.Add(res);
i = k;
break;
}
}
}
}
string bankFilePath = System.IO.Path.Combine(MyG lobals.BAS E_DIR, MyGlobals.BASE_FILE_Name + ".ard.out");
using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
{
using (BinaryWriter bwrite = new BinaryWriter(fs))
{
foreach (var arr in result1)
{
bwrite.Write(arr);
}
}
}
sw1.Close();
How would you rewrite the following routine so that only the first element of an array is written out? I only need the first element to be written out. Ideally I just need the 1st string of data to be written out and no longer need an array of elements.
--------------------------
private static void ProcessFileBinary(string filePath, StreamWriter sw1)
{
var anchor1 = Encoding.UTF8.GetBytes("II
var anchor2 = Encoding.UTF8.GetBytes("55
List<byte[]> result1 = new List<byte[]>();
var bytes = File.ReadAllBytes(filePath
for (int i = 0; i < bytes.Length; i++)
{
var data = bytes.Skip(i).Take(anchor1
if (data.SequenceEqual(anchor
{
for (int k = i + anchor1.Length; k < bytes.Length; k++)
{
var data2 = bytes.Skip(k).Take(anchor2
if (data2.SequenceEqual(ancho
{
var res = bytes.Skip(i).Take(k - i).ToArray();
result1.Add(res);
i = k;
break;
}
}
}
}
string bankFilePath = System.IO.Path.Combine(MyG
using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
{
using (BinaryWriter bwrite = new BinaryWriter(fs))
{
foreach (var arr in result1)
{
bwrite.Write(arr);
}
}
}
sw1.Close();
ASKER
Yes, that is what I mean:
How would you implement "bwrite.Write(arr[0]);" in the following:
------------------
using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
{
using (BinaryWriter bwrite = new BinaryWriter(fs))
{
foreach (var arr in result1)
{
bwrite.Write(arr);
}
}
}
How would you implement "bwrite.Write(arr[0]);" in the following:
------------------
using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
{
using (BinaryWriter bwrite = new BinaryWriter(fs))
{
foreach (var arr in result1)
{
bwrite.Write(arr);
}
}
}
using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
{
using (BinaryWriter bwrite = new BinaryWriter(fs))
{
foreach (var arr in result1)
{
bwrite.Write(arr[0]);
}
}
}
{
using (BinaryWriter bwrite = new BinaryWriter(fs))
{
foreach (var arr in result1)
{
bwrite.Write(arr[0]);
}
}
}
ASKER
How do I get rid of the error:
The name 'arr' does not exist in the current context?
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------
string bankFilePath = System.IO.Path.Combine(MyG lobals.BAS E_DIR, MyGlobals.BASE_FILE_Name + ".ard.out");
using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
{
using (BinaryWriter bwrite = new BinaryWriter(fs))
{
bwrite.Write(arr[0]);
}
}
The name 'arr' does not exist in the current context?
--------------------------
string bankFilePath = System.IO.Path.Combine(MyG
using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
{
using (BinaryWriter bwrite = new BinaryWriter(fs))
{
bwrite.Write(arr[0]);
}
}
did you still get the error with the original code?
bwrite.Write(arr);
bwrite.Write(arr);
ASKER
In the following code, does it make sense to use "foreach" when I only want the first element?
using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
{
using (BinaryWriter bwrite = new BinaryWriter(fs))
{
foreach (var arr in result1)
{
bwrite.Write(arr[0]);
}
}
}
using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
{
using (BinaryWriter bwrite = new BinaryWriter(fs))
{
foreach (var arr in result1)
{
bwrite.Write(arr[0]);
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
bwrite.Write(arr); ?
try this:
bwrite.Write(arr[0]);