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

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("55ELRC");
            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(anchor1))
                {
                    for (int k = i + anchor1.Length; k < bytes.Length; k++)
                    {
                        var data2 = bytes.Skip(k).Take(anchor2.Length);
                        if (data2.SequenceEqual(anchor2))
                        {
                            var res = bytes.Skip(i).Take(k - i).ToArray();
                            result1.Add(res);
                            i = k;
                            break;
                        }
                    }
                }
            }
            string bankFilePath = System.IO.Path.Combine(MyGlobals.BASE_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();
Avatar of Anwar Saiah
Anwar Saiah

do you mean this:
bwrite.Write(arr); ?
 try this:
bwrite.Write(arr[0]);
Avatar of zimmer9

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);
                    }
                }
            }
using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
            {
                using (BinaryWriter bwrite = new BinaryWriter(fs))
                {
                    foreach (var arr in result1)
                    {
                        bwrite.Write(arr[0]);
                    }
                }
            }
Avatar of zimmer9

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(MyGlobals.BASE_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]);
                }
            }
did you still get the error with the original code?
bwrite.Write(arr);
Avatar of zimmer9

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]);
                    }
                }
            }
ASKER CERTIFIED SOLUTION
Avatar of Anwar Saiah
Anwar Saiah

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