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
874 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
[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
  • 2
4 Comments
 
LVL 56

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 56

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

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