Solved

How would you modify a function to write out 2 statements only for the 1st occurrence of a program loop in C# using VS2010?

Posted on 2014-01-07
1
443 Views
Last Modified: 2014-01-07
I am writing a C# console application in VS2010.

How would you modify the following function so that the 2 lines marked with asterisks are
only executed for the 1st program loop?

This way, these 2 WriteLine statements are only written out the 1st time throught the loop.
==========================================================================

private static void ProcessFile(string filePath, StreamWriter sw1)
        {
            for (int i = 0; i < bytes.Length; i++)
            {                
                if (data.SequenceEqual(anchor1))
                {                  
                    for (int k = i + anchor1.Length; k < bytes.Length; k++)
                    {

                        if (data2.SequenceEqual(anchor2))
                        {
                            string indFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.ind");
                            using (StreamWriter sw = new StreamWriter(indFilePath, true))
                            {
Execute once>**      sw.WriteLine("COMMENT: specify code page of the index date");
Execute once>**      sw.WriteLine("CODEPAGE:819");
                                sw.WriteLine(commentchecknum);
                                sw.WriteLine(groupNameCheckNumber);                                
                                sw.Close();
                            }
                            MyGlobals.intcheckOffset = MyGlobals.intcheckOffset + intcheckimagelength;
                            break;
                        }
                    }
                    sw1.Close();
                }
            }
        }
0
Comment
Question by:zimmer9
1 Comment
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39764167
Hi zimmer9;

This modification to the code should do what you need.

private static void ProcessFile(string filePath, StreamWriter sw1)
{
    bool firstTimeFlag = true;
    for (int i = 0; i < bytes.Length; i++)
    {                
        if (data.SequenceEqual(anchor1))
        {                   
            for (int k = i + anchor1.Length; k < bytes.Length; k++)
            {

                if (data2.SequenceEqual(anchor2))
                {
                    string indFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.ind");
                    using (StreamWriter sw = new StreamWriter(indFilePath, true))
                    {
                        if( firstTimeFlag )
                        {
                            sw.WriteLine("COMMENT: specify code page of the index date");
                            sw.WriteLine("CODEPAGE:819");
                            firstTimeFlag = false;
                        }
                        sw.WriteLine(commentchecknum);
                        sw.WriteLine(groupNameCheckNumber);                                
                        sw.Close();
                    }
                    MyGlobals.intcheckOffset = MyGlobals.intcheckOffset + intcheckimagelength;
                    break;
                }
            }
            sw1.Close();
        }
    }
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 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