title: Using StreamReader and StreamWriter to Modify Files
body: <p>I am trying to use StreamReader and StreamWriter to Open a text file (fixed width) and to modify a few specific columns of data. I have dates with the following format that are going to be converted to packed COMP-3 fields. </p>
  
  <pre><code>020100718F
  020100716F
  020100717F
  020100718F
  020100719F
  </code></pre>
  
  <p>I want to be able to read in the dates form a file using StreamReader, then convert them to packed fields (5 characters), and then output them using StreamWriter. However, I haven't found a way to use StreamWriter to right to a specific position, and beginning to wonder if is possible.</p>
  
  <p>I have the following code snip-it.</p>
  
  <pre><code>System.IO.StreamWriter writer;
  
  this.fileName = @"C:\Test9.txt";
  reader = new System.IO.StreamReader(System.IO.File.OpenRead(this.fileName));
  
  currentLine = reader.ReadLine();
  currentLine = currentLine.Substring(30, 10);    //Substring Containing the Date
  reader.Close();
  
  ...
  // Convert currentLine to Packed Field 
  ...
  
  writer = new System.IO.StreamWriter(System.IO.File.Open(this.fileName, System.IO.FileMode.Open));
  writer.Write(currentLine);
  </code></pre>
  
  <p>Currently what I have does the following:</p>
  
  <pre><code>After:
  !@#$%0718F
  020100716F
  020100717F
  020100718F
  020100719F 
  
  !@#$% = Ascii Characters SO can't display 
  </code></pre>
  
  <p>Any ideas? Thanks!</p>
  
  <p><strong>UPDATE</strong>
  Information on Packed Fields COMP-3</p>
  
  <p>Packed Fields are used by COBOL systems to reduce the number of bytes a field requires in files. Please see the following SO post for more information: <a href="https://stackoverflow.com/questions/8974091/sql-how-to-get-comp-3-packed-decimal">Here</a></p>
  
  <p>Here is Picture of the following date "20120123" packed in COMP-3. This is my end result and I have included because I wasn't sure if it would effect possible answers.</p>
  
  <p><img src="https://i.stack.imgur.com/If9Gn.png" alt="Picture of the following date 20120123 packed"></p>
  
  <p>My question is how do you get StreamWriter to dynamically replace data inside a file and change the lengths of rows?</p>