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
854 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 52

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

12 Experts available now in Live!

Get 1:1 Help Now