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
445 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 79

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

How our DevOps Team Maximize Uptime

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

Question has a verified solution.

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

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 …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

825 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