?
Solved

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
2
Medium Priority
?
491 Views
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
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 2000 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:
http://stackoverflow.com/questions/4122153/how-to-write-some-text-as-bytes-without-encoding
0
 

Author Comment

by:zimmer9
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
sample
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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