Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:32:53+00:00 2026-05-28T23:32:53+00:00

I am trying to use StreamReader and StreamWriter to Open a text file (fixed

  • 0

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.

020100718F
020100716F
020100717F
020100718F
020100719F

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.

I have the following code snip-it.

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);

Currently what I have does the following:

After:
!@#$%0718F
020100716F
020100717F
020100718F
020100719F 

!@#$% = Ascii Characters SO can't display 

Any ideas? Thanks!

UPDATE
Information on Packed Fields COMP-3

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: Here

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.

Picture of the following date 20120123 packed

My question is how do you get StreamWriter to dynamically replace data inside a file and change the lengths of rows?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:32:53+00:00Added an answer on May 28, 2026 at 11:32 pm

    I have always found it better to to read the input file, filter/process the data and write the output to a temporary file. After finished, delete the original file (or make a backup) and copy the temporary file over. This way you haven’t lost half your input file in case something goes wrong in the middle of processing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use a StreamReader to read a file, but it is
I am having problems trying to use the file.Close with StreamWriter in this method,
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
Trying to use GnuPG with Delphi (Win32). I need to sign some file with
Hi i was trying to filter a large of text file using Regex.Split with
I am trying to use the HTML Agility pack to scrape some data from
I have to text file in the following format : Word [tab][tab] Word [Carriage
I'm trying to encrypt a text file using Perl and then decrypt it using
I'm trying to read the contents of a text file, in this case a
I'm trying to understand how to use WCF Data Services (based on EF 4.1)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.