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
441 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in page 3 45
Error in JQuery 5 37
Counting connections to SQL Server through C# 3 27
Need a starter for ETL protocol? 4 39
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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