Solved

How would you modify a routine so that only the first element of an array is written out in a C# console appplication?

Posted on 2014-03-10
7
259 Views
Last Modified: 2014-03-11
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();
0
Comment
Question by:zimmer9
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:aboo_s
ID: 39918121
do you mean this:
bwrite.Write(arr); ?
 try this:
bwrite.Write(arr[0]);
0
 

Author Comment

by:zimmer9
ID: 39918148
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);
                    }
                }
            }
0
 
LVL 10

Expert Comment

by:aboo_s
ID: 39918212
using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
            {
                using (BinaryWriter bwrite = new BinaryWriter(fs))
                {
                    foreach (var arr in result1)
                    {
                        bwrite.Write(arr[0]);
                    }
                }
            }
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:zimmer9
ID: 39918213
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]);
                }
            }
0
 
LVL 10

Expert Comment

by:aboo_s
ID: 39918276
did you still get the error with the original code?
bwrite.Write(arr);
0
 

Author Comment

by:zimmer9
ID: 39918319
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]);
                    }
                }
            }
0
 
LVL 10

Accepted Solution

by:
aboo_s earned 500 total points
ID: 39918363
using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
            {
                using (BinaryWriter bwrite = new BinaryWriter(fs))
                {
                   
                   
                        bwrite.Write(result1[0]);
                   
                }
            }
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question