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
488 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
2 Comments
 
LVL 20

Accepted Solution

by:
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:
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linq, c# 8 35
C# Formatting DateTime String Value 5 52
Query in AngularJS 8 48
How to reference a column by name when iterating through a GridView ? 1 34
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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 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