c# - StreamWriter appends random data -
I am seeing strangely behavior using the code by writing additional data to the StreamWriter class using this code:
Public Zero WriteToCSV (string filename) {StreamWriter streamWriter = null; {StreamWriter = Try new streamer (filename); Log InEfO ("Writing CSV Report Header Information ..."); StreamWriter.WriteLine ("\" {0} \ ", \" {1} \ ", \" {2} \ ", \" {3} \ "", ((int) CSVRecordType.Header) .toString (" D2 ", Culture Info. Contentculture), M_InputFilename, M_LoadStartDate, M_load and Date); Int recordCount = 0; If (summary summary) {log info ("writing CSV report summary section ..."); ("\" {0}, \ "{1} {\"}, \ "\" {\ "{0} "{2} \", \ "{3} \", ((int) CSVRecordType.Summary) .toString ("D2", CultureInfo.CurrentCulture), categoryResult.Value.StatusString, categoryResult.Value.Count.ToString (CultureInfo.CurrentCulture), categoryResult.Value .club); RecordCount ++; }} LogInfo ("writing cases of CSV report cases ..."); Leading (keywalkularPayer & lt; KeyWellPayer & lt; LoadStatus, string & gt ;, category result & amp; gt; Category Mascotload related.) Results of data load {foreach (result in class CaseLoadResult.Result.Value.CaseLoadResults result) {if ( (LoadStatus.Success == Result. Status and successes) | (LoadStatus.Warnings == result.Status & WarningCases) || (LoadStatus.Failure == result.Status & amp; FailureCases). | (LoadStatus.NotProcessed == result.Status & amp; NotProcessedCases)) {StreamWriter.Write ("" \ "{0} \", \ "{1} \", \ "{2} \", \ "{3} \", \ "{4} \" ", ((Integer CSVRecordType.Result) .toString (" D2 ", CultureInfo.CurrentCulture), result.Status, result. UniqueId, result.Category, result.ClassicReference); If (rarospons) {streamWriter.Write (", \" {0} \ ", result. Responsexil); } StreamWriter.WriteLine (); RecordCount ++; }}} StreamWriter.WriteLine ("\" {0} \ ", \" {1} \ "", ((int) CSVRecordType.Count.) Ostring ("D2", CultureInfo.CurrentCulture), recordcount); Log in ("CSV report was written in '{0}', filename); } Hold (IOException execution) {string errorMessage = string.Format (Unable to write XML file '{0}', filename); Log.Error (errorMessage); Log.Error (exception.Message); New Miexation Throw (Error Message, Exception);} Finally {if (zero! = Stream Vector) {streamWriter.Close ();
Each row in the generated file records from 0 to N For example:
[record zero] [record one] ... [record n]
however the end of the output file Attached to For example:
[Record Zero] [Record One] ... [Record n] [many null]
[Record Zero] [Record One] ... [Record N] [Half Full Record]
or
[Record Zero]
It also happens in different parts of the code which also uses the serial class. Besides, the files prepare all sizes which are multiples of 1024. On any other machine L am unable to reproduce the behavior and I have tried to recreate the environment.
EDIT: Added additional code.
After
Two situations which jumped into mind when talking about garbage at the end of a stream: Not short when overwriting: If you overwrite a file, but write fewer data, then you have to minimize it, while opening the file there is a surcharge to do this, or You can use theStream.SetLength
.
MemoryStream
- you should use or . ToArray ()
(to get the correct number of bytes), or use .getbuffer ()
but only Copy Bytes out of buffer (anything past is garbage)
I'm guessing "1" applies here?
Comments
Post a Comment