0

I have a file which is very long, and has no line breaks, CR or LF or other delimiters.

Records are fixed length, and the first control record length is 24 and all other record lengths are of fixed length 81 bytes.

I know how to read a fixed length file per line basis and I am using Multi Record Engine and have defined classes for each 81 byte line record but can’t figure out how I can read 80 characters at a time and then parse that string for the actual fields.

Hogstrom
  • 3,581
  • 2
  • 9
  • 25
NewDev
  • 1
  • 2
    FileSream read method takes a length paramter, just read 80 bytes at a time – pm100 Jun 30 '22 at 22:13
  • And this (https://stackoverflow.com/questions/26060441/reading-data-from-fixed-length-file-into-class-objects) should give you some hints on how to map your 80 byte buffers into C# types. – Flydog57 Jun 30 '22 at 22:16
  • Thanks for the response. I need to read 80 characters and save it to a string. Using File stream length parameter doesn’t return the object – NewDev Jul 01 '22 at 00:04
  • Two questions on the content. Sequential MF files are either fixed or variable. Since you indicate that the first record is 24 bytes long and the subsequent records are 81 bytes it begs the question what is the format of the source file. Do you know the DCB attributes on the mainframe? – Hogstrom Jul 06 '22 at 13:37

1 Answers1

0

You can use the FileStream to read the number of bytes you need - like in your case either 24 or 81. Keep in mind that progressing through the stream the position changes and therefor you should not use the offset (should always be 0) - also be aware that if there is no information "left" on the stream it will cause an exception. So you would end up with something like this:

        var recordlength = 81;
        var buffer = new byte[recordlength];
        stream.Read(buffer, 0, recordlength); // offset = 0, start at current position
        var record = System.Text.Encoding.UTF8.GetString(buffer); // single record

Since the recordlength is different for the control record you could use that part into a single method, let's name it Read and use that read method to traverse through the stream untill you reach the end, like this:

    public List<string> Records()
    {
        var result = new List<string>();
        using(var stream = new FileStream(@"c:\temp\lipsum.txt", FileMode.Open))
        {
            // first record
            result.Add(Read(stream, 24));
            var record = "";
            do
            {
                record = Read(stream);
                if (!string.IsNullOrEmpty(record)) result.Add(record);
            }
            while (record.Length > 0);
        }
        return result;
    }

    private string Read(FileStream stream, int length = 81)
    {
        if (stream.Length < stream.Position + length) return "";
        var buffer = new byte[length];
        stream.Read(buffer, 0, length);
        return System.Text.Encoding.UTF8.GetString(buffer);
    }

This will give you a list of records (including the starting control record).

This is far from perfect, but an example - also keep in mind that even if the file is empty there is always 1 result in the returned list.

riffnl
  • 3,248
  • 1
  • 19
  • 32