Solved

How to write output records to a fixed length file using C# code in a console application with VS2010?

Posted on 2013-06-20
4
846 Views
Last Modified: 2013-06-23
I am working with VS2010 and C#. I have attached an input file that has no file exension.
This file has fixed length records of 252 bytest that start with the sequence
"51ELRC" in the first 6 positons. These records can exist starting in any location within the input file. How would your write C# code as a console application to extract all 252 byte records that start with the character sequence "51ELRC" and write them to the output
file titled output.txt with a fixed record length of 252 bytes?
sample
0
Comment
Question by:zimmer9
  • 2
  • 2
4 Comments
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 39267840
Your input records are not all 252 bytes - the records that don't start with 51ELRC are 252 bytes - there are 6 of them but then you get teh first target record which is 282 bytes followed by another that is 271 bytes and another that is 318 bytes.

There does not appear to be anything in the record that indicates a size.
0
 

Author Comment

by:zimmer9
ID: 39268444
I am attaching a replacement input file. Sorry for the record layout problem with my original input file. In the new file:

I opened this file in TEXTPAD and observed the following:

the first record for type 51ELRC is in positions 1513 through 1764
the second record for type 51ELRC is in positions 19 through 270
the third record for type 51ELRC is in positions 13 through 264
the fourth record for type 51ELRC is in positions 41 through 292
Copy-of-sample
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39269273
I am not seeing that and your numbers don't make sense.

You have records overlapping.

In the attached file I have the first record at Hex: 0x5E8
The second record starts at 0x703
Size: 283 bytes

The third record starts 0x81E
Size: 283bytes

The fourth record starts at 0x94D
Size: 303 Bytes

End of file is at 0xA4B
Size: 254 Bytes

Each of the target records seems to be terminated by a J* followed by a variable number of CR/LF and spaces.

If you remove the CR/LF and spaces then the file shows the records as fixed length - this is where the challange is - how to read the lines as binary but ignoring the white space.

One solution is to read the file into memory (without knowing more about your data and how big these files will get - this may not be the best solution). Assuming you can do this - you then process the input by looping through the data 1 byte at a time looking for the key sequence. You then copy out the 252 bytes you need and write to the file - jump to the end of the record and continue scanning bytes.
0
 

Author Comment

by:zimmer9
ID: 39269788
Okay then, since you are not seeing the record layout as I describe it, we can just focus on the record layout as follows, for the records that have the value "51ELRC" in starting positions 1 through 6 and records of these type have a fixed size length of 252 byes. Text fields are in ASCII format. The records with the value "51ELRC" are present throughtout the file in various starting positions.

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

707 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now