I am using VS2010 with C# console application and was wondering how to write fixed length records to an output file based on an input file with text and binary data?

Posted on 2013-06-23
Last Modified: 2013-07-12
How would you write 252 byte fixed length records to an output file using C# code in a console application with VS2010?
I am working with VS2010 and C#. I have an input file that has no file exension.
This file contains records of various types.
However, I am interested in extracting only 252 byte fixed length records from this file
that start with the sequence "51ELRC" in the first 6 positons of each record of this type.
Occurences of this record type can exist starting in any location within
the input file. How would you write C# code as a console application to extract all 252
byte records from this input file that start with the character sequence "51ELRC"
and write these record types to an output file titled output.txt as fixed record length records
of 252 bytes?

The input record layout is as follows for the records that have the value "51ELRC" in
starting positions 1 through 6.
The text fields are in ASCII format.
The records with the value type "51ELRC" in positions 1 through 6 are present throughtout
the input file in various locations within this file.

column         data type              positions
---------      -------------       ------------
Field 1        Text                 1  -   46
Field 2        Binary               47 -  118
Field 3        Text                 119
Field 4        Binary               120 - 132
Field 5        Text                 133 - 149
Field 6        Binary               150 - 162
Field 7        Text                 163 - 170
Field 8        Binary               171 - 183
Field 9        Text                 184 - 203
Field 10       Binary               204 - 216
Field 11       Text                 217 - 228
Field 12       Binary               229 - 241
Field 13       Text                 242 - 252
Question by:zimmer9
LVL 20

Accepted Solution

Daniel Van Der Werken earned 500 total points
ID: 39271942
It would be helpful if you included a sample input file with the data you want extracted. I'm also not sure I understand the request overall. First off, is the input file larger than 252 bytes?

Basically, since it's binary and text data, I'd use a BinaryReader to read the file. Then, at each offset, if you want text, I'd convert the binary data to text with Convert.ToBase64String method.

Writing it out might be a bit of a challenge as you're attempting to mix text and binary information in a single file. I am not honestly sure how that would work out for the best You may simply want to keep the binary formatting for the output and have the binary representation of the text written out to the file and keep it in binary format.

Recommend this article:

Author Comment

ID: 39272150
I have attached an example file titled Sample that contains 3 detail records that have the value "51ELRC" in their first 6 positions for each record of this particular type. These record types are scattered throughout the file. My goal is to output all of the occurences of these record type "51ELRC"  records into an output file comprised of purely these records.

The records of type "51ELRC" are followed by the character string JJ*

So the output file would contain 252 byte records as follows where the ... represents
positions 7 through 242

51ELRC    ...          0000036934
51ELRC    ...          0000093745
51ELRC    ...          0000083846

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question