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
256 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now