views:

159

answers:

1

Hi

I'm seeing odd behaviour using the StreamWriter class writing extra data to a file using this code:

public void WriteToCSV(string filename)
{
    StreamWriter streamWriter = null;
    try
    {
        streamWriter = new StreamWriter(filename);
        Log.Info("Writing CSV report header information ... ");
        streamWriter.WriteLine("\"{0}\",\"{1}\",\"{2}\",\"{3}\"", ((int)CSVRecordType.Header).ToString("D2", CultureInfo.CurrentCulture), m_InputFilename, m_LoadStartDate, m_LoadEndDate);

        int recordCount = 0;

        if (SummarySection)
        {
            Log.Info("Writing CSV report summary section ... ");
            foreach (KeyValuePair<KeyValuePair<LoadStatus, string>, CategoryResult> categoryResult in m_DataLoadResult.DataLoadResults)
             {
                streamWriter.WriteLine("\"{0}\",\"{1}\",\"{2}\",\"{3}\"", ((int)CSVRecordType.Summary).ToString("D2", CultureInfo.CurrentCulture), categoryResult.Value.StatusString, categoryResult.Value.Count.ToString(CultureInfo.CurrentCulture), categoryResult.Value.Category);
                recordCount++;
             }
        }

        Log.Info("Writing CSV report cases section ... ");
        foreach (KeyValuePair<KeyValuePair<LoadStatus, string>, CategoryResult> categoryResult in m_DataLoadResult.DataLoadResults)
        {
            foreach (CaseLoadResult result in categoryResult.Value.CaseLoadResults)
            {
                if ((LoadStatus.Success == result.Status && SuccessCases) ||
                    (LoadStatus.Warnings == result.Status && WarningCases) ||
                    (LoadStatus.Failure == result.Status && FailureCases) ||
                    (LoadStatus.NotProcessed == result.Status && NotProcessedCases))
                {
                    streamWriter.Write("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\"", ((int)CSVRecordType.Result).ToString("D2", CultureInfo.CurrentCulture), result.Status, result.UniqueId, result.Category, result.ClassicReference);
                    if (RawResponse)
                    {
                        streamWriter.Write(",\"{0}\"", result.ResponseXml);
                    }
                    streamWriter.WriteLine();
                    recordCount++;
                }
            }
        }

        streamWriter.WriteLine("\"{0}\",\"{1}\"", ((int)CSVRecordType.Count).ToString("D2", CultureInfo.CurrentCulture), recordCount);

        Log.Info("CSV report written to '{0}'", fileName);
    }
    catch (IOException execption)
    {
        string errorMessage = string.Format(CultureInfo.CurrentCulture, "Unable to write XML report to '{0}'", fileName);
        Log.Error(errorMessage);
        Log.Error(exception.Message);
        throw new MyException(errorMessage, exception);
    }
    finally
    {
        if (null != streamWriter)
        {
            streamWriter.Close();
        }
    }
}

The file produced contains a set of records on each line 0 to N, for example:

[Record Zero]
[Record One]
...
[Record N]

However the file produced either contains nulls or incomplete records from further up the file appended to the end. For example:

[Record Zero]
[Record One]
...
[Record N]
[Lots of nulls]

or

[Record Zero]
[Record One]
...
[Record N]
[Half complete records]

This also happens in separate pieces of code that also use the StreamWriter class. Furthermore, the files produced all have sizes that are multiples of 1024. I've been unable to reproduce this behaviour on any other machine and have tried recreating the environment. Previous versions of the application didn't exhibite this behaviour despite having the same code for the methods in question.

EDIT: Added extra code.

A: 

There are two scenarios that leap to mind when talking about garbage at the end of a stream:

  1. not truncating when over-writing: if you overwrite a file but write less data, then you must truncate it. There is an overload to do this when opening the file, or you can use theStream.SetLength.
  2. writing buffer padding; in particular, the most common mistake is when using MemoryStream - you must either use .ToArray() (to get the correct number of bytes), or use .GetBuffer() but only copy .Length bytes out of the buffer (anything past that is garbage)

I'm guessing "1" applies here?

Marc Gravell
streamWriter = new StreamWriter(filename); uses FileMode.Create. This will always truncate the file. Do you know a scenario when this might not be true?
zespri
I don't agree with @zespri. Taken from the docs, in the constructor of StreamWriter(String): If the file exists, it is overwritten; otherwise, a new file is created.
Timores
@Timores - a quick check seems to show that it is overwritten and (correctly) truncated. So scratch that idea.
Marc Gravell