?
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
Medium Priority
?
453 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 82

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

777 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