Click here to Skip to main content
16,005,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code:
C#
FileInfo FIFileIn = new FileInfo(@"C:\TestData.csv");
StreamReader FileIn = FIFileIn.OpenText();
string FileInStr;
string[] FileInTokens;
bool FileOutOpen = false;

// Skip past the column heading line (1st line) in the input file.
FileInStr = FileIn.ReadLine();

DateTime PrevReading = DateTime.MinValue;
string PrevReadingStr = PrevReading.ToShortDateString();

// FileInfo FIFileOut = new FileInfo(string.Empty);
// StreamWriter FileOut = FIFileOut.OpenWrite();

StreamWriter FileOut;

while (!FileIn.EndOfStream)
{
  FileInStr = FileIn.ReadLine();
  FileInTokens = FileInStr.Split(',');

  bool TokenIsDate = true;
  DateTime DTReading = new DateTime();
  string DTReadingStr = DTReading.ToString();

  try
  {
    DTReading = DateTime.Parse(FileInTokens[1]);
  }
  catch
  {
    TokenIsDate = false;
  }

  if (TokenIsDate)
  {
    DTReadingStr = DTReading.ToShortDateString();
    if (DTReadingStr != PrevReadingStr)
    {
      string CSVFilename =
        @"C:\RRTest" +
        DTReading.Year.ToString() +
        DTReading.Month.ToString() +
        DTReading.Day.ToString();

      if (FileOutOpen)
      {
        if (FileOut != null)
          FileOut.Close();
      }

      FileOut = new StreamWriter(CSVFilename);
      FileOutOpen = true;

      DTReadingStr = DTReading.ToShortDateString();
      PrevReadingStr = DTReadingStr;
    }

    FileOut.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", FileInTokens[0], FileInTokens[1], FileInTokens[2], FileInTokens[3], FileInTokens[4], FileInTokens[5]));
  }
}

FileIn.Close();
if (FileOut != null)
  FileOut.Close();


I wonder how I can declare FileOut, so that I don't have a compiler error on the final close, and I don't have all these ugly "if (FileOut != null)" statements.

Any advice/pointers much appreciated!

Richard
Posted
Updated 5-Jan-12 22:45pm
v2

I suggest to refactor your code.

1. Process the input file and put the results in a list. You might want to use LINQ GroupBy and select the first one
2. a. Use TPL (Task Parallel Library) to write the items to files using a lambda
OR
b. Loop the list and write items (use lambda)

Use the using[^] statement to handle the close for you
 
Share this answer
 
Why are they "ugly"?
They are there for a good reason - to ensure that if you have opened a file, that you close it properly.
You could move the code into a method, if it upsets you that much, but I would leave it pretty much as it is, possibly just changing
C#
if (FileOutOpen)
{
  if (FileOut != null)
    FileOut.Close();
}
to
C#
if (FileOutOpen && FileOut != null)
    {
    FileOut.Close();
    }
And please, be consistent with your braces!
If you need braces in the statement part of one if that has a single statement, then you should have braces on the statement of the contained statement:
C#
if (FileOutOpen)
{
  if (FileOut != null)
    FileOut.Close();
}
Would be more consistent as:
C#
if (FileOutOpen)
{
  if (FileOut != null)
  {
    FileOut.Close();
  }
}

Mixing styles is not a good idea!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900