Solved

How to modify a C# console application so that an output file is opened only once at the beginning of a FOR LOOP?

Posted on 2014-01-26
9
436 Views
Last Modified: 2014-01-27
I am developing a C# console application using VS2010.

I have attached my source code.

The application reads a check image file that has binary and text fields and writes out 3 different output files.

How would you modify the following 2 statements that are in the method

"private static void ProcessFile(string filePath, StreamWriter sw1)"

so that the following 2 statements are processed ONLY ONCE at the beginning of the FOR LOOP?
--------------------------------------------------------------------------------------------------

MODIFY -->        string indFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.ind");
                            using (StreamWriter sw = new StreamWriter(indFilePath, true))
0
Comment
Question by:zimmer9
9 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 39811077
no attached sample code.. move the lines above the for/loop
0
 

Author Comment

by:zimmer9
ID: 39811079
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39811091
move the definitions outside the for loop:

 string indFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.ind");
StreamWriter sw = new StreamWriter(indFilePath, true);
for ...

Open in new window

change the using line as follows :

using (sw)
    ....

Open in new window

0
 

Author Comment

by:zimmer9
ID: 39811104
string indFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.ind");
            StreamWriter sw = new StreamWriter(indFilePath, true);

            for (int i = 0; i < bytes.Length; i++)
            {
              ...

              if (firstTimeFlag)
              {
                    sw.WriteLine("COMMENT: specify code page of the index date");
                    sw.WriteLine("CODEPAGE:819");
                    firstTimeFlag = false;
              }
                               
Error On Next Line    Cannot write to a closed TextWriter

              sw.WriteLine(stringb.ToString());

              stringb.Length = 0;

              sw.Close();
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39811114
ok move the using statement outside the for loop and remove the using statement withing the for loop:

string indFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.ind");
 using(StreamWriter sw = new StreamWriter(indFilePath, true));
{
            for (int i = 0; i < bytes.Length; i++)
            {
              ...

              if (firstTimeFlag)
              {
                    sw.WriteLine("COMMENT: specify code page of the index date");
                    sw.WriteLine("CODEPAGE:819");
                    firstTimeFlag = false;
              }
                               
              sw.WriteLine(stringb.ToString());

              stringb.Length = 0;

              sw.Close(); 
          }
}

Open in new window

0
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 400 total points
ID: 39811120
you need to move the close outside the for loop as well:

string indFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.ind");
 using(StreamWriter sw = new StreamWriter(indFilePath, true));
{
            for (int i = 0; i < bytes.Length; i++)
            {
              ...

              if (firstTimeFlag)
              {
                    sw.WriteLine("COMMENT: specify code page of the index date");
                    sw.WriteLine("CODEPAGE:819");
                    firstTimeFlag = false;
              }
                               
              sw.WriteLine(stringb.ToString());

              stringb.Length = 0;

          }
          sw.Close(); 
} 

Open in new window

0
 

Author Comment

by:zimmer9
ID: 39811132
using(StreamWriter sw = new StreamWriter(indFilePath, true));
Warning: Possible mistaken empty statement

---------------------------------------------------------------------------------------------


Error      2,3,4,5,6,     The name 'sw' does not exist in the current context      
---------------------------------------------------------------------------------------------

              if (firstTimeFlag)
              {
                    sw.WriteLine("COMMENT: specify code page of the index date");
                    sw.WriteLine("CODEPAGE:819");
                    firstTimeFlag = false;
              }
                               
              sw.WriteLine(stringb.ToString());

              stringb.Length = 0;

          }
          sw.Close();
}
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 100 total points
ID: 39811191
To correct the code (typo) from Paul Jackson you need to remove the semi colon at the end of the using(....) line of code.  Otherwise that terminates the using block and leads to the error you experience.
0
 

Author Comment

by:zimmer9
ID: 39811925
Great Andy,

Your suggestion eliminated the Error "The name 'sw' does not exist in the current context"

The current error is as follows:
---------------------------------------

Cannot write to a closed TextWriter.

sw.WriteLine(stringb.ToString());
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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