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
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 81

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Technology Partners: 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

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 …
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 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