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.
-
2FileSream read method takes a length paramter, just read 80 bytes at a timepm100– pm1002022年06月30日 22:13:22 +00:00Commented Jun 30, 2022 at 22:13
-
And this (stackoverflow.com/questions/26060441/…) should give you some hints on how to map your 80 byte buffers into C# types.Flydog57– Flydog572022年06月30日 22:16:32 +00:00Commented Jun 30, 2022 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 objectNewDev– NewDev2022年07月01日 00:04:37 +00:00Commented Jul 1, 2022 at 0: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– Hogstrom2022年07月06日 13:37:51 +00:00Commented Jul 6, 2022 at 13:37
1 Answer 1
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.