Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
265 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

885 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