File I/O for EBCDIC/packed files - read/write

G

Guest

Hi,
I have read the "how to's" on MSDN including this one,
http://msdn2.microsoft.com/en-us/library/36b93480.aspx, on reading/writing
files in C#, but haven't found something that I can put together that is
working.

The file I have is in EBCDIC, rec length = 250 and delimited by CRLF's.

What I want to do is open an EBCDIC file in Read/Write mode so I can read
the record, alter the data in a fixed set of positions (position 2, zero
based for 19 positions), and re-write the record (same file).

I have:
private const string FILE_NAME = "d:\\somefile.txt" // file that is in EBCDIC
// Create the reader for data.
FileStream fs = new FileStream(FILE_NAME, FileMode.Open,FileAccess.ReadWrite);
BinaryReader br = new BinaryReader(fs); // ??? use this or something else?

I am aware of the "Encoding" class, but am not certain if it's needed for
what I want to do.

I am also aware of the "MemoryStream" and "BinaryReader" classes, but not
sure which to use.

Any links to thorough examples or responses with code snippets would be most
useful.
Thanks.
 
J

Jon Skeet [C# MVP]

I have read the "how to's" on MSDN including this one,http://msdn2.microsoft.com/en-us/library/36b93480.aspx, on reading/writing
files in C#, but haven't found something that I can put together that is
working.

I am aware of the "Encoding" class, but am not certain if it's needed for
what I want to do.

Yes it is.

To read, use a StreamReader with the appropriate encoding. To write,
use a StreamWriter with the appropriate encoding.

I have an EBCDIC encoding class at http://pobox.com/~skeet/csharp/ebcdic
which lets you specify various different variants of it.

Jon
 
G

Guest

Jon,

Thanks for the quick reply - I will look it over and try to work on it this
week.

One question I have that hopefully you can answer:

When I specify Encoding ebcdic = Encoding.GetEncoding("IBM01140");

Is there way to see the actual table for this particular encoding, perhaps
in a *.h file?

The reason I ask, is we have an internal table that we use for ASCII-EBCDIC
conversions, based on a table from one of our vendors. I am sure it is a
"flavor" of the IBM conversion, but I would like to compare it to what
Microsoft is doing to see if it matches our table. It won't do me any good
to use the wrong encoding for the object.

Or I would need to know how to create my own file that I pass in to the
"GetEncoding" function which is what I'm guessing your doing with "using
JonSkeet.Ebcdic;" from your link.

Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

You could use Reflector to look into the encoding class, but if it is
making any calls to the underlying OS to handle the encoding (which in this
case, I imagine it is since you are requesting a code page) then you aren't
going to be able to figure it out.

However, you can do a comparison by cycling through the range of byte
values that are valid for the encoding, and seeing what the encoding spits
out for them. If they match your table, then use it.

If not, you can always create your own Encoding type, derived from
Encoding, so that it encodes/decodes the bytes the way you need it to.
 
J

Jon Skeet [C# MVP]

j.a. harriman said:
Thanks for the quick reply - I will look it over and try to work on it this
week.

One question I have that hopefully you can answer:

When I specify Encoding ebcdic = Encoding.GetEncoding("IBM01140");

Is there way to see the actual table for this particular encoding, perhaps
in a *.h file?

The reason I ask, is we have an internal table that we use for ASCII-EBCDIC
conversions, based on a table from one of our vendors. I am sure it is a
"flavor" of the IBM conversion, but I would like to compare it to what
Microsoft is doing to see if it matches our table. It won't do me any good
to use the wrong encoding for the object.

Or I would need to know how to create my own file that I pass in to the
"GetEncoding" function which is what I'm guessing your doing with "using
JonSkeet.Ebcdic;" from your link.

The tables I use are taken from http://std.dkuug.dk/i18n/charmaps/ but
you can fairly easily use your own table if you rebuild my code. If you
look in the CharMapReader directory in the source.zip file you'll find
CharMapReader.cs which documents the file format (same as the site
linked above). You can put your own file in there, rebuild the
resources etc, and away you go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top