I am trying to use StreamReader and StreamWriter to open a text file (fixed width) and to modify a few specific data columns. I have dates with the following format that will be converted to packed COMP-3 fields.
020100718F 020100716F 020100717F 020100718F 020100719F
I want to be able to read a file in the form of a date using StreamReader, then convert them to packed fields (5 characters), and then output them using StreamWriter. However, I did not find a way to use StreamWriter to access a specific position and start to wonder if this is possible.
I have the following snip-it 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);
I currently have the following:
After: !@ #$%0718F 020100716F 020100717F 020100718F 020100719F !@ #$% = Ascii Characters SO can't display
Any ideas? Thanks!
UPDATE Packed Field Information COMP-3
Packed fields are used by COBOL systems to reduce the number of bytes required for files in files. See the following SO publication for more information: Here
Here is a picture of the next date "20120123", packaged on COMP-3. This is my final result, and I included it because I was not sure that this would affect the possible answers.

My question is: how to get StreamWriter to dynamically replace the data inside the file and change the length of the lines?
source share